Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Looking at the TableColumnModel interface, you can see that methods are provided to query the state of the column model. This includes the number of columns in the model, the margin for each column, the total column width, the index of a column, the column at an index, and an Enumeration of all columns. The table’s column structure can be changed dynamically. Methods are provided to add and remove columns as well as to move columns.

As you saw earlier in this chapter, the TableColumnModel contains a ListSelectionListener to manage column selection. The model defines methods for setting and getting the selection model, as well as convenience methods for manipulating column selection status and querying the selection set.

Finally, you see that TableColumnModelListeners can be registered with the model. This allows views or other interested classes to be notified when the column model changes. Calling one of the methods defined in the TableColumnModelListener interface shown in Listing 12.5 performs the notification. Each method in the interface delivers an instance of the TableColumnModelEvent, also shown in the listing. It is interesting to note the different styles of listener interfaces and events between the TableModelListener/TableModelEvent pair presented earlier in this section and the TableColumnModelListener/TableColumnModelEvent pair shown here. The TableModelListener defines a single method, and the event delivered provides the type information. The TableColumnModelListener interface defines multiple methods, one for each type of change in the column model, and the event simply provides the index of effected column. The first style makes implementing the interface easier, but requires a more complex method to decode and handle the event. The second style requires more work to implement the interface, but the purpose of each method is clearly defined. Both styles of listener/event pairs are valid, but one would expect the same style to be used with such closely related models.

Listing 12.5 The TableColumnModelListener and TableColumnModelEvent Classes

public interface TableColumnModelListener extends java.util.EventListener
{
    /** Tells listeners that a column was added to the model. */
    public void columnAdded(TableColumnModelEvent e);

    /** Tells listeners that a column was removed from the model. */
    public void columnRemoved(TableColumnModelEvent e);

    /** Tells listeners that a column was repositioned. */
    public void columnMoved(TableColumnModelEvent e);

    /** Tells listeners that a column was moved due to a margin change.*/
    public void columnMarginChanged(ChangeEvent e);

    /**
     * Tells listeners that the selection model of the
     * TableColumnModel changed.
     */
    public void columnSelectionChanged(ListSelectionEvent e);
}

public class TableColumnModelEvent extends java.util.EventObject
{
//
//  Instance Variables
//

    /** The index of the column from where it was moved or removed */
    protected int
fromIndex;

    /** The index of the column to where it was moved or added from */
    protected int
toIndex;

//
// Constructors
//

    public TableColumnModelEvent(TableColumnModel source,
        int from, int to) {
        super(source);
        fromIndex = from;
        toIndex = to;
    }

//
// Querying Methods
//

    /** Returns the fromIndex.  Valid for removed or moved events */
    public int getFromIndex() { return fromIndex; };

    /** Returns the toIndex.  Valid for add and moved events */
    public int getToIndex() { return toIndex; };
}

The TableColumnModel consists of a set of TableColumn instances. When a column is retrieved from the model, it is an instance of the TableColumn class. This class encapsulates the information about the appearance of a column in the table. It contains attributes for the column width, minimum and maximum width, and whether it is resizable or not. The minimum and maximum values are enforced when the user is resizing the column by dragging its border. This class also can contain a renderer and editor. This allows each column in the table to use its own renderer and editor. Later in this chapter, you will see how it is possible to set a renderer and editor for a given type for the entire table. Setting the value on the column overrides the value set for the table. This gives the developer total control over how the data in a column is displayed and edited. The column header’s renderer can also be set. A PropertyChangeListener can be registered with the TableColumn instance to be notified of changes to these properties. Table 12.4 contains a complete list of bound properties contained in the TableColumn class. The constants used for the property names are defined in the TableColumn class.

Table 12.4 Bound Properties of the TableColumn Class

Property Name Setter Method Getter Method

HEADER_RENDERER_PROPERTY setHeaderRenderer getHeaderRenderer
HEADER_VALUE_PROPERTY setHeaderValue getHeaderValue
CELL_RENDERER_PROPERTY setCellRenderer getCellRenderer
COLUMN_WIDTH_PROPERTY setWidth getWidth


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.