• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable

 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic