Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
Training Index

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 JavaTM source files represent a solution to this Magercise:

Short Course

About This Short Course

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


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.