Java Reference
In-Depth Information
JLabel retval = new JLabel();
// ...
if (row == 2) {
retval.setBackground(Color.GREEN);
} else {
retval.setBackground(Color.WHITE);
}
return retval;
The full implementation of a renderer can also take into account whether
or not the cell is selected and/or has focus. This has to do with enabling mouse
clicks to select either that particular cell or the row or column containing that
cell. You will likely want to render the cell differently (with a darker color,
perhaps) to show that it has been selected. Whatever the renderer, you set up
and then return a GUI component whose attributes (font, color, size, and
so on) are used to display that cell.
We hope you get the idea—there is a lot more to renderers than we will
cover here. The Java Tutorial covers them more, and the Javadoc pages have
some introduction, too.
Similar to renderers are editors. When a user clicks in a table cell, the table
may allow him or her to edit its contents. A cell editor is needed to do that,
and then your program needs to do something with the value that was entered.
For our BudgetPro example we avoid this complexity by disallowing the user
to enter anything into the table—our table is for display only. We do this on
lines 187-191 by overriding the method isCellEditable() to always return
false :
187 public boolean
188 isCellEditable(int row, int col)
189 {
190 return false;
191 } // is CellEditable
Notice that the method is passed the row and column means that you
could make some cells editable and some not.
16.7.2.10
Let's look at the last part of the table that we implement for BudgetPro:
Selection Listeners
194 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Search WWH ::




Custom Search