First complete the data model skeleton.
The data model for a JTable
needs to implement the
TableModel
interface. The AbstractTableModel
class
implements this and only requires you to implement two of the methods. So,
have the ADataModel
class extend AbstractTableModel
.
The four methods AbstractTableModel
leaves to complete are
getRowCount()
to return the number of rows in the data model,
getColumnCount()
to return the number of columns,
getColumnName()
if you do not like the default name provided
(like spreadsheet notation A, B, C, ... , AA, BB, ...), and
getValueAt()
to return the data at a particular row/column
intersection. In the skeleton, getRowCount()
,
getColumnCount()
, and getColumnName()
are complete.
However, getValueAt()
needs to be done. Using the row
parameter holding the row number, the column
parameter holding
the column number, and the rows
two-dimension array, have
getValueAt()
return the appropriate data.
The CustomDisplayer needs to setup the
JTable
. First, you need to create an instance of the data model
ADataModel
.
Next, you need to create a JTable
with the data model as the
parameter to the constructor.
To have the last column display a custom cell renderer, you need to get the
column and change its default cell renderer. Use the ColorizedCell
class as the cell renderer. You'll complete that next.
Create a JScrollPane
for the table and add it to the screen.
To install the custom cell renderer, have the
ColorizedCell class be a table cell renderer.
Also, complete the setValue()
method of the renderer's class. The
Object
parameter of setValue()
is the text string
for the desired color. Instead of showing the text though, the
ColorizedCell
should display the color in an oval. Using the
ColorizedIcon
inner class provided, create an Icon
of the appropriate color and display it as the renderer's icon.
Save everything and compile the program. Then run it to see the results.