开发者

JTable row selection without changing the data

Hi Im doing a project for college and have a JTable in my GUI. I want the user to be able to select rows without being able to change the data 开发者_如何学Cin the table. I am using two arrays to crate the table not the table model. Thanks

I dont want to use tablemodels!!!!


You can implement the isEditable() method in your table model and return false and your table will not be editable:

public class MyTableModel extends AbstractTableModel{
 public void isEditable(){
   return false;
 }
}

Then you have a table and you set its model to a MyTableModel object eg.

JTable table = new JTable();
table.setModel(new MyTableModel());

Extra Information:

AbstractTableModel.isCellEditable(int,int) returns false by default, so you don't need to override it to get that behavior.

The DefaultTableModel implementation of that method returns true by default, so that one must be overriden if you wish to make cells un-editable.

Resource of this answer.


You should define your own table model, by extending AbstractTableModel, or DefaultTableModel. Just override isCellEditable(int row, int col) and make it return false.


You will need to override the table model's isCellEditable() method to always return false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜