Definitive Guide to Swing for Java 2

Definitive Guide to Swing for Java 2

by John Zukowski
     
 


  • Shows how to create graphical user interfaces with the 1.3 version of the Swing Component Set
  • Offers a clear treatment of the Model-View-Controller architecture
  • Demonstrates the creation and customization of reusable Swing Component extensions
  • Explores the Swing text and html package, and JFC drag-and-drop

Overview


  • Shows how to create graphical user interfaces with the 1.3 version of the Swing Component Set
  • Offers a clear treatment of the Model-View-Controller architecture
  • Demonstrates the creation and customization of reusable Swing Component extensions
  • Explores the Swing text and html package, and JFC drag-and-drop capabilities
  • Provides in-depth coverage of Jtree and Jtable

Completely updated for the 1.3 edition of the Java 2 Platform, the second edition of this widely praised book is the one-stop resource for serious Java developers. This book shows programmers those parts of the Swing Component Set they will use on a daily basis to create graphical user interfaces. Youll learn about the Model-View-Controller architecture that lies behind all Swing components, and youll discover how to customize the components for specific environments. Youll be provided with custom editors and renderers for use with tables, trees, and list components. You'll also find an overview of the Swing architecture, event handling with the Swing Component Set, core Swing components, toggleable components, Swing menus and toolbars, borders, pop-ups and choosers, and more.

Editorial Reviews

From the Publisher
From the reviews of the first edition:
"...I didn't really expect to get much out of yet another Swing book. To my great surprise, John Zukowski has put together a clearly documented, insightful, comprehensive, and entertaining exploration of the Swing API, complete with the kind of treatment that offers something new to even the most sophisticated Swing developer. What's more, the way the material is presented makes this book easily accessible to even novice Swing developers, so I can't help but recommend it highly for anyone who has, or plans to do, any Java user interface development...."
(DevX)

Product Details

ISBN-13:
9781893115781
Publisher:
Apress
Publication date:
11/05/2000
Edition description:
2nd ed. 2000
Pages:
890
Product dimensions:
7.50(w) x 9.25(h) x 1.83(d)

Read an Excerpt

Chapter 17: Tables

Chapter 16 took an in-depth look at the Swing JTree component. In this Chapter, we'll examine the many details of the JTable component. The component is the standard Swing component for displaying two-dimensional data in the form of a grid, as shown in Figure 17-1.

NOTE All the examples in this Chapter will work fine without configuring your environment to display Japanese fonts. However, instead of seeing the ideographs, you will see characters such as question marks or boxes, depending upon your platform. In order to see the Kanji ideographs in the sample programs, you will need to be using the Japanese version of the font.properties file (properties.ja ) and have the necessary Japanese fonts installed. In addition to the Windows NT installation CD, you can find the necessary Windows fonts at http://ftp.monash.edu.au/pub/nihongo/ ie3lpkja.exe. Solaris users must contact Sun to request the Asian outline fonts for Solaris environments. More on adding fonts to the Java runtime environment is at http://java.sun.com/products/jdk/1.3/docs/guide/intl/ fontprop.html.

Figure 17-1: Sample JTable...

Introducing Tables

Like the JTree component, the JTable component relies on numerous support classes for its inner workings. For the JTable , the support classes are found in the javax.swing.table package. The cells within the JTable can be selected by row, column, row and column, or individual cell. It's the responsibility of the current ListSelectionModel settings to control the selection within a table.

The display of the different cells within a table is the responsibility of the TableCellRenderer; the DefaultCellRenderer offers one such implementation of the TableCellRenderer interface in a JLabel subclass.

Managing the data stored in the cells is accomplished through an implementation of the TableModel interface. The AbstractTableModel provides the basics of an implementation of the interface without any data storage. By comparison, the DefaultTableModel encapsulates the TableModel interface and uses a vector of vectors for the data storage. You extend AbstractTableModel if you need a different type of storage than the kind supplied by the DefaultTableModel, for instance, if you already had the data in your own data structure.

The TableColumnModel interface and the DefaultTableColumnModel implementation of the interface manage the table's data as a series of columns. They work together with the TableColumn class to allow for greater flexibility in manipulating individual columns. For example, you can store columns of data in the TableModel in an order that's different than the display order within the JTable. The TableColumnModel manages a second ListSelectionModel to control table column selection.

At the top of every column is a column header. By default, the TableColumn class relies on the JTableHeader class to render a text column header. Nevertheless, you must embed the JTable in a scroll pane to see the default header.

Cells within a JTable can be editable. If a cell is editable, how the editing works depends on the TableCellEditor implementation, such as the DefaultCellEditor implementation, which extends from AbstractCellEditor . In addition, no classes exist to handle individual rows. Rows must be manipulated on a cell-by-cell basis. However, behind the scenes, the JTable does use the SizeSequence utility class to deal with variable height rows. However, you won't need to manipulate it yourself.

Figure 17-2 shows these class interrelationships.

NOTE There are additional interrelationships among the elements used by the JTable component. These relationships will be explored later in this chapter with each specific interface and class.

To visualize how the JTable elements all fit together, examine Figure 17-3.

Class JTable

We'll first look at the JTable class, which gives you a way to display data in tabular form....

Meet the Author

John Zukowski has been involved with the Java platform since it was just called Java, 11 years and running, since 1995. He is actively working with SavaJe Technologies to finish up the JavaOne 2006 device of show: the Jasper S20 mobile phone. He currently writes a monthly column for Sun's Core Java Technologies Tech Tips and Technology Fundamentals Newsletter. He has contributed content to numerous other sites, including jGuru, DevX, Intel, and JavaWorld. He has written many other popular titles on Java, including Java AWT Reference (O'Reilly), Mastering Java 2 (Sybex), Borlands' JBuilder: No Experience Required (Sybex), Learn Java with JBuilder 6 (Apress), Java Collections (Apress), and The Definitive Guide to Swing (Apress).

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >