Joe's Java and Web Jottings

your host


Your Host

Calendar

««Nov 2009»»
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
2930

My Top Tags

               

Java Libs and Tools

My Other Pages

My RSS Feeds








FAQ: What's the easiest way to...

posted Thursday, 23 January 2003
What's the easiest way to make an entire column uneditable in a JTable?

assuming DefaultTableModel? in a subclass of DefaultTableModel, add this:


Set nonEditableColumns = new HashSet();

public void setColumnEditable(int col, boolean editable)
{
if (editable) nonEditableColumns.add(new Integer(col);
else nonEditableColumns.remove(new Integer(col)); // its ok if its not there
}

public boolean isCellEditable(int row, int col)
{
return nonEditableColumns.contains(new Integer(col));
}



if its not already a subsclass, it will have to become one in order
to support the new method of setColumnEditable, or you can just internalize that
into the class that contains the model.

that is, the Set and setColumnEditable methods are in the class that
constructs the model, and the isCellEditable is part of an anonymous inner
class.


DefaultTableModel dtm = new DefaultTableModel()
{
public boolean isCellEditable(int row, int col)
{ ... }
};