Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views1 page

Javanotes5 667

Uploaded by

ds student
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Javanotes5 667

Uploaded by

ds student
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

12.4.

COMPLEX COMPONENTS AND MVC 651

public boolean isCellEditable(int rowNum, columnNum)


where rowNum and columnNum are the position of a cell in the grid of rows and columns that
make up the table. When the controller wants to know whether a certain cell is editable, it
calls this method in the table model. If the return value is true, the user is allowed to edit the
cell.
The default model that is used when the table is created, as above, from an array of objects
allows editing of all cells. For this model, the return value of isCellEditable() is true in all
cases. To make some cells non-editable, you have to provide a different model for the table. One
way to do this is to create a subclass of DefaultTableModel and override the isCellEditable()
method. (DefaultTableModel and some other classes that are discussed in this section are
defined in the package javax.swing.table.) Here is how this might be done in the “states and
capitals” program to make all cells non-editable:
TableModel model = new DefaultTableModel(statesAndCapitals,columnHeads) {
public boolean isCellEditable(int row, int col) {
return false;
}
};
JTable table = new JTable(model);
Here, an anonymous subclass of DefaultTableModel is created in which the isCellEditable()
method returns false in all cases, and the model object that is created from that class is passed
as a parameter to the JTable constructor.
The DefaultTableModel class defines many methods that can be used to modify the table,
including for example: setValueAt(item,rowNum,colNum) to change the item in a given cell;
removeRow(rowNum) to delete a row; and addRow(itemArray) to add a new row at the end of
the table that contains items from the array itemArray. Note that if the item in a given cell
is null, then that cell will be empty. Remember, again, that when you modify the model, the
view is automatically changed to reflect the changes.
In addition to the isCellEditable() method, the table model method that you are most
likely to want to override is getColumnClass(), which is defined as
public Class<?> getColumnClass(columnNum)
The purpose of this method is to specify what kind of values are allowed in the specified
column. The return value from this method is of type Class. (The “<?>” is there for technical
reasons having to do with generic programming. See Section 10.5, but don’t worry about
understanding it here.) Although class objects have crept into this book in a few places—
in the discussion of ClassLoaders in Subsection 12.1.3 for example—this is the first time we
have directly encountered the class named Class. An object of type Class represents a class.
A Class object is usually obtained from the name of the class using expressions of the form
“Double.class” or “JTable.class”. If you want a three-column table in which the column
types are String, Double, and Boolean, you can use a table model in which getColumnClass is
defined as:
public Class<?> getColumnClass(columnNum) {
if (columnNum == 0)
return String.class;
else if (columnNum = 1)
return Double.class;
else
return Boolean.class;

You might also like