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
  • 168,117
  • 40
  • 217
  • 433
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
  • 678,734
  • 91
  • 1,224
  • 1,255
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
  • 5,341
  • 2
  • 21
  • 31