Technical Support
Discussion Forum
Online Training
Technical Articles
Bug Parade
JDC Resources
DukeDollars
Early Access
Toolbar
Community Process
Technology Sites

JDC Home Page

Solaris Developer Connection

Java Technology
java.sun.com

Java Industry Connection

Log Out

Online Training
SearchFAQContact Us

Magercise 4: Help

[Magercise 4 | API Docs | Short Course | Magercises]

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 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.

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 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.

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 model ADataModel.

ADataModel myModel = new ADataModel();

Task 4

Next, you need to create a JTable with 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 ColorizedCell class 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 JScrollPane for 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. 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.

First, extend the DefaultTableCellRenderer class. 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.

Return to Magercise 4

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.





Questions?
29-Mar-99
Copyright © 1996-1999 Sun Microsystems Inc.
All Rights Reserved. Legal Terms. Privacy Policy.
Sun Microsystems