3

I have an JAVA project and want to make my JTable with a DefaultTableModel non-editable. I know a work-around to do this, called:

JTable table = new JTable(...){  
  public boolean isCellEditable(int row, int column){  
    return false;  
  }  
};  

Like said: i dont like this. This is not according the rules of my school training.

Is there any way to do this? Maybe is there a good way. I hope so!

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
Dave
  • 77
  • 1
  • 2
  • 10

2 Answers2

10

You should not subclass the JTable itself, but the table model:

DefaultTableModel myModel = new DefaultTableModel(...) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}

Or even better, don't use a DefaultTableModel, and use an AbstractTableModel that directly gets the information in your business objects rather than copying all the information from the business objects to Vectors.

JB Nizet
  • 657,433
  • 87
  • 1,179
  • 1,226
2

select Jtable , and don't forget to create table model (DefaultTableModel TableModel)

JTable table_1 = new JTable (TableModel){public boolean isCellEditable(int row,int column)  
        {switch(column){             
           case 4:  // select the cell you want make it not editable 
             return false;  
         default: return true;}  
        }}; 
Adel
  • 4,146
  • 2
  • 18
  • 29