| 
Help is available for each task, or you can go straight to
the solution 
source code.
Task 1 
First complete the data model skeleton. 
The data model for a JTableneeds to implement theTableModelinterface. TheAbstractTableModelclass implements this and only requires you to implement two of the 
methods. So, have theADataModelclass extendAbstractTableModel. 
 
Import the Swing table package.
import javax.swing.table.*;
 
and, add the following to the class definition line:
 extends AbstractTableModel
 Task 2
The four methods AbstractTableModelleaves to complete aregetRowCount()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, ...), andgetValueAt()to return the data at a particular row/column 
intersection. In the skeleton,getRowCount(),getColumnCount(), andgetColumnName()are complete. 
However,getValueAt()needs to be done. Using therowparameter holding the row number, thecolumnparameter holding 
the column number, and therowstwo-dimension array, havegetValueAt()return the appropriate data. 
public Object getValueAt (int row, int column) {
  return rows[row][column];
}
If you wanted to support an editable data model, you would also need to 
override the behavior of
setValueAt(). Task 3
The CustomDisplayer needs to setup the 
JTable. First, you need to create an instance of the data modelADataModel. 
ADataModel myModel = new ADataModel();
 Task 4
Next, you need to create a JTablewith the data model as the 
parameter to the constructor. 
JTable table = new JTable (myModel);
 Task 5
To have the last column display a custom cell renderer, you need to get the 
column and change its default cell renderer. Use the ColorizedCellclass as the cell renderer. You'll complete that next. 
 
To get a column, first you need to get the TableColumnModel:TableColumnModel tcm = table.getColumnModel();
 
Get the column from the column model:
 TableColumn column = 
  tcm.getColumn (myModel.getColumnCount()-1);
 
Create cell renderer and associate with column:
 TableCellRenderer renderer = new ColorizedCell();
column.setCellRenderer (renderer);
 Task 6
Create a JScrollPanefor the table and add it to the screen. 
JScrollPane scrollPane = 
  new JScrollPane  (table);
add (scrollPane, BorderLayout.CENTER);
 Task 7
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. TheObjectparameter ofsetValue()is the text string 
for the desired color. Instead of showing the text though, theColorizedCellshould display the color in an oval. Using theColorizedIconinner class provided, create anIconof the appropriate color and display it as the renderer's icon. 
 
First, extend the DefaultTableCellRendererclass. Then, the color 
lookup code is already complete. All that remains to be done is creating the 
icon and associating it to the renderer.Icon icon = new ColorizedIcon (c);
setIcon (icon);
 Task 8
Save everything and compile the program. Then run it to see the results.
 
javac CustomDisplayer.java
java CustomDisplayer
 
The dependency checker will cause the other classes to be compiled.
 
Copyright © 1998-1999 
MageLang Institute.
All Rights Reserved.
 |