my dog learned polymorphism*
The moose likes Java in General and the fly likes JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "JTable" Watch "JTable" New topic
Author

JTable

Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
I have 7 colums in a JTable.
I want the first 6 to be uneditable and the last to be editable.

How do you do this with a JTable?

I had to extend it to get the functionality but I'm sure there must ba an easier way.

class MyJTable extends JTable {
MyJTable(String[][] data, String[] columnNames) {
super(data, columnNames);
}

public boolean isCellEditable(int row, int col) {
if (col < 6) {
return false;
} else {
return true;
}
}
}
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
For that type of behavior you would want to create your own table model. For such a little change you could probably do it in an anonymous inner class that subclasses DefaultTableModel.

If you want more control over the model you can create a subclass of AbstractTableModel, which is not to difficult. The main advantage I see with AbstractTableModel vs DefaultTableModel is picking your own storage. DefaultTableModel uses a Vector, which is fine for most small/medium tables.

Sun has a tutorial on building table models here.
 
GeeCON Prague 2014
 
subject: JTable