DefaultTableModel make cell not editable JTable

17,426

Solution 1

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.

Solution 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;}  
        }}; 
Share:
17,426
Dave
Author by

Dave

Updated on June 24, 2022

Comments

  • Dave
    Dave almost 2 years

    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!

  • Dave
    Dave over 11 years
    Thanks, this works 4 me!