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:
Using JTable

[Help | API Docs | Short Course | Magercises]

Skeleton Code

In this magercise, you will create a JTable that has two columns, one the name of a person, the other a graphical representation of their favorite color. Using classes within the javax.swing.table package, you'll complete a data model and a custom cell renderer for a TableColumn.

Tasks:

  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.

  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.

  3. The CustomDisplayer needs to setup the JTable. First, you need to create an instance of the data model ADataModel.

  4. Next, you need to create a JTable with the data model as the parameter to the constructor.

  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.

  6. Create a JScrollPane for the table and add it to the screen.

  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.

  8. Save everything and compile the program. Then run it to see the results.

[Result]

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution

The following Java™ source files represent a solution to this Magercise:

Short Course

About This Short Course

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