Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DefaultTableModel make cell not editable JTable [duplicate]

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!

like image 928
Dave Avatar asked Apr 09 '26 19:04

Dave


2 Answers

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.

like image 171
JB Nizet Avatar answered Apr 12 '26 09:04

JB Nizet


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;}  
        }}; 
like image 40
Adel Avatar answered Apr 12 '26 10:04

Adel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!