FileDocCategorySizeDatePackage
JTableHeader.javaAPI DocJava SE 6 API47720Tue Jun 10 00:26:56 BST 2008javax.swing.table

JTableHeader

public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible
This is the object which manages the header of the JTable.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.76 01/17/07
author
Alan Chung
author
Philip Milne
see
javax.swing.JTable

Fields Summary
private static final String
uiClassID
protected JTable
table
The table for which this object is the header; the default is null.
protected TableColumnModel
columnModel
The TableColumnModel of the table header.
protected boolean
reorderingAllowed
If true, reordering of columns are allowed by the user; the default is true.
protected boolean
resizingAllowed
If true, resizing of columns are allowed by the user; the default is true.
protected boolean
updateTableInRealTime
Obsolete as of Java 2 platform v1.3. Real time repaints, in response to column dragging or resizing, are now unconditional.
protected transient TableColumn
resizingColumn
The index of the column being resized. null if not resizing.
protected transient TableColumn
draggedColumn
The index of the column being dragged. null if not dragging.
protected transient int
draggedDistance
The distance from its original position the column has been dragged.
private TableCellRenderer
defaultRenderer
The default renderer to be used when a TableColumn does not define a headerRenderer.
Constructors Summary
public JTableHeader()
Constructs a JTableHeader with a default TableColumnModel.

see
#createDefaultColumnModel

 

//
// Constructors
//

                     
      
	this(null);
    
public JTableHeader(TableColumnModel cm)
Constructs a JTableHeader which is initialized with cm as the column model. If cm is null this method will initialize the table header with a default TableColumnModel.

param
cm the column model for the table
see
#createDefaultColumnModel

	super();

	//setFocusable(false); // for strict win/mac compatibility mode,
                               // this method should be invoked

	if (cm == null)
	    cm = createDefaultColumnModel();
	setColumnModel(cm);

	// Initialize local ivars
	initializeLocalVars();

	// Get UI going
	updateUI();
    
Methods Summary
public voidcolumnAdded(javax.swing.event.TableColumnModelEvent e)
Invoked when a column is added to the table column model.

Application code will not use these methods explicitly, they are used internally by JTable.

param
e the event received
see
TableColumnModelListener

 resizeAndRepaint(); 
public intcolumnAtPoint(java.awt.Point point)
Returns the index of the column that point lies in, or -1 if it lies out of bounds.

return
the index of the column that point lies in, or -1 if it lies out of bounds

        int x = point.x;
        if (!getComponentOrientation().isLeftToRight()) {
            x = getWidthInRightToLeft() - x;
        }
        return getColumnModel().getColumnIndexAtX(x);
    
public voidcolumnMarginChanged(javax.swing.event.ChangeEvent e)
Invoked when a column is moved due to a margin change.

Application code will not use these methods explicitly, they are used internally by JTable.

param
e the event received
see
TableColumnModelListener

 resizeAndRepaint(); 
public voidcolumnMoved(javax.swing.event.TableColumnModelEvent e)
Invoked when a column is repositioned.

Application code will not use these methods explicitly, they are used internally by JTable.

param
e the event received
see
TableColumnModelListener

 repaint(); 
public voidcolumnRemoved(javax.swing.event.TableColumnModelEvent e)
Invoked when a column is removed from the table column model.

Application code will not use these methods explicitly, they are used internally by JTable.

param
e the event received
see
TableColumnModelListener

 resizeAndRepaint(); 
public voidcolumnSelectionChanged(javax.swing.event.ListSelectionEvent e)
Invoked when the selection model of the TableColumnModel is changed. This method currently has no effect (the header is not redrawn).

Application code will not use these methods explicitly, they are used internally by JTable.

param
e the event received
see
TableColumnModelListener

 
protected javax.swing.table.TableColumnModelcreateDefaultColumnModel()
Returns the default column model object which is a DefaultTableColumnModel. A subclass can override this method to return a different column model object

return
the default column model object

	return new DefaultTableColumnModel();
    
protected javax.swing.table.TableCellRenderercreateDefaultRenderer()
Returns a default renderer to be used when no header renderer is defined by a TableColumn.

return
the default table column renderer
since
1.3

	return new DefaultTableCellHeaderRenderer();
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JTableHeader. For JTableHeaders, the AccessibleContext takes the form of an AccessibleJTableHeader. A new AccessibleJTableHeader instance is created if necessary.

return
an AccessibleJTableHeader that serves as the AccessibleContext of this JTableHeader

	if (accessibleContext == null) {
	    accessibleContext = new AccessibleJTableHeader();
	}
	return accessibleContext;
    
public javax.swing.table.TableColumnModelgetColumnModel()
Returns the TableColumnModel that contains all column information of this table header.

return
the columnModel property
see
#setColumnModel

	return columnModel;
    
public javax.swing.table.TableCellRenderergetDefaultRenderer()
Returns the default renderer used when no headerRenderer is defined by a TableColumn.

return
the default renderer
since
1.3

	return defaultRenderer;
    
public javax.swing.table.TableColumngetDraggedColumn()
Returns the the dragged column, if and only if, a drag is in process, otherwise returns null.

return
the dragged column, if a drag is in process, otherwise returns null
see
#getDraggedDistance

	return draggedColumn;
    
public intgetDraggedDistance()
Returns the column's horizontal distance from its original position, if and only if, a drag is in process. Otherwise, the the return value is meaningless.

return
the column's horizontal distance from its original position, if a drag is in process, otherwise the return value is meaningless
see
#getDraggedColumn

	return draggedDistance;
    
public java.awt.RectanglegetHeaderRect(int column)
Returns the rectangle containing the header tile at column. When the column parameter is out of bounds this method uses the same conventions as the JTable method getCellRect.

return
the rectangle containing the header tile at column
see
JTable#getCellRect

        Rectangle r = new Rectangle(); 
	TableColumnModel cm = getColumnModel(); 

	r.height = getHeight();	

	if (column < 0) { 
	    // x = width = 0; 
            if( !getComponentOrientation().isLeftToRight() ) {
		r.x = getWidthInRightToLeft(); 
            }
	}
	else if (column >= cm.getColumnCount()) { 
            if( getComponentOrientation().isLeftToRight() ) {
		r.x = getWidth(); 
            }
	}
	else { 
            for(int i = 0; i < column; i++) { 
                r.x += cm.getColumn(i).getWidth();
            }
            if( !getComponentOrientation().isLeftToRight() ) {
                r.x = getWidthInRightToLeft() - r.x - cm.getColumn(column).getWidth();
            }

	    r.width = cm.getColumn(column).getWidth(); 
	}
	return r; 
    
public booleangetReorderingAllowed()
Returns true if the user is allowed to rearrange columns by dragging their headers, false otherwise. The default is true. You can rearrange columns programmatically regardless of this setting.

return
the reorderingAllowed property
see
#setReorderingAllowed

	return reorderingAllowed;
    
public booleangetResizingAllowed()
Returns true if the user is allowed to resize columns by dragging between their headers, false otherwise. The default is true. You can resize columns programmatically regardless of this setting.

return
the resizingAllowed property
see
#setResizingAllowed

	return resizingAllowed;
    
public javax.swing.table.TableColumngetResizingColumn()
Returns the resizing column. If no column is being resized this method returns null.

return
the resizing column, if a resize is in process, otherwise returns null

	return resizingColumn;
    
public javax.swing.JTablegetTable()
Returns the table associated with this header.

return
the table property

	return table;
    
public java.lang.StringgetToolTipText(java.awt.event.MouseEvent event)
Allows the renderer's tips to be used if there is text set.

param
event the location of the event identifies the proper renderer and, therefore, the proper tip
return
the tool tip for this component

	String tip = null;
	Point p = event.getPoint();
	int column;

	// Locate the renderer under the event location
	if ((column = columnAtPoint(p)) != -1) {
	    TableColumn aColumn = columnModel.getColumn(column);
	    TableCellRenderer renderer = aColumn.getHeaderRenderer(); 
            if (renderer == null) { 
                renderer = defaultRenderer; 
            }
	    Component component = renderer.getTableCellRendererComponent(
			      getTable(), aColumn.getHeaderValue(), false, false,
			      -1, column);

	    // Now have to see if the component is a JComponent before
	    // getting the tip
	    if (component instanceof JComponent) {
		// Convert the event to the renderer's coordinate system
		MouseEvent newEvent;
		Rectangle cellRect = getHeaderRect(column);

		p.translate(-cellRect.x, -cellRect.y);
		newEvent = new MouseEvent(component, event.getID(),
					  event.getWhen(), event.getModifiers(),
					  p.x, p.y, event.getXOnScreen(), event.getYOnScreen(),
                                          event.getClickCount(),
					  event.isPopupTrigger(), MouseEvent.NOBUTTON);

		tip = ((JComponent)component).getToolTipText(newEvent);
	    }
	}

	// No tip from the renderer get our own tip
	if (tip == null)
	    tip = getToolTipText();

	return tip;
    
public javax.swing.plaf.TableHeaderUIgetUI()
Returns the look and feel (L&F) object that renders this component.

return
the TableHeaderUI object that renders this component

	return (TableHeaderUI)ui;
    
public java.lang.StringgetUIClassID()
Returns the suffix used to construct the name of the look and feel (L&F) class used to render this component.

return
the string "TableHeaderUI"
return
"TableHeaderUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

	return uiClassID;
    
public booleangetUpdateTableInRealTime()
Obsolete as of Java 2 platform v1.3. Real time repaints, in response to column dragging or resizing, are now unconditional.

	return updateTableInRealTime;
    
private intgetWidthInRightToLeft()

	if ((table != null) &&
	    (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF)) {
	    return table.getWidth();
	}
	return super.getWidth();
    
protected voidinitializeLocalVars()
Initializes the local variables and properties with default values. Used by the constructor methods.

        setOpaque(true);
	table = null;
	reorderingAllowed = true;
	resizingAllowed = true;
	draggedColumn = null;
	draggedDistance = 0;
	resizingColumn = null;
	updateTableInRealTime = true;

	// I'm registered to do tool tips so we can draw tips for the
	// renderers
	ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
	toolTipManager.registerComponent(this);
	setDefaultRenderer(createDefaultRenderer()); 
    
protected java.lang.StringparamString()
Returns a string representation of this JTableHeader. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Overriding paramString to provide information about the specific new aspects of the JFC components.

return
a string representation of this JTableHeader

        String reorderingAllowedString = (reorderingAllowed ?
					  "true" : "false");
        String resizingAllowedString = (resizingAllowed ?
					"true" : "false");
        String updateTableInRealTimeString = (updateTableInRealTime ?
					      "true" : "false");

        return super.paramString() +
        ",draggedDistance=" + draggedDistance +
        ",reorderingAllowed=" + reorderingAllowedString +
        ",resizingAllowed=" + resizingAllowedString +
        ",updateTableInRealTime=" + updateTableInRealTimeString;
    
public voidresizeAndRepaint()
Sizes the header and marks it as needing display. Equivalent to revalidate followed by repaint.

        revalidate();
	repaint();
    
public voidsetColumnModel(javax.swing.table.TableColumnModel columnModel)
Sets the column model for this table to newModel and registers for listener notifications from the new column model.

param
columnModel the new data source for this table
exception
IllegalArgumentException if newModel is null
see
#getColumnModel
beaninfo
bound: true description: The object governing the way columns appear in the view.

        if (columnModel == null) {
            throw new IllegalArgumentException("Cannot set a null ColumnModel");
        }
        TableColumnModel old = this.columnModel;
        if (columnModel != old) {
            if (old != null) {
                old.removeColumnModelListener(this);
	    }
            this.columnModel = columnModel;
            columnModel.addColumnModelListener(this);

	    firePropertyChange("columnModel", old, columnModel);
            resizeAndRepaint();
        }
    
public voidsetDefaultRenderer(javax.swing.table.TableCellRenderer defaultRenderer)
Sets the default renderer to be used when no headerRenderer is defined by a TableColumn.

param
defaultRenderer the default renderer
since
1.3

	this.defaultRenderer = defaultRenderer;
    
public voidsetDraggedColumn(javax.swing.table.TableColumn aColumn)
Sets the header's draggedColumn to aColumn.

Application code will not use this method explicitly, it is used internally by the column dragging mechanism.

param
aColumn the column being dragged, or null if no column is being dragged

	draggedColumn = aColumn;
    
public voidsetDraggedDistance(int distance)
Sets the header's draggedDistance to distance.

param
distance the distance dragged

	draggedDistance = distance;
    
public voidsetReorderingAllowed(boolean reorderingAllowed)
Sets whether the user can drag column headers to reorder columns.

param
reorderingAllowed true if the table view should allow reordering; otherwise false
see
#getReorderingAllowed
beaninfo
bound: true description: Whether the user can drag column headers to reorder columns.

	boolean old = this.reorderingAllowed; 
	this.reorderingAllowed = reorderingAllowed; 
	firePropertyChange("reorderingAllowed", old, reorderingAllowed); 
    
public voidsetResizingAllowed(boolean resizingAllowed)
Sets whether the user can resize columns by dragging between headers.

param
resizingAllowed true if table view should allow resizing
see
#getResizingAllowed
beaninfo
bound: true description: Whether the user can resize columns by dragging between headers.

 
	boolean old = this.resizingAllowed; 
	this.resizingAllowed = resizingAllowed; 
	firePropertyChange("resizingAllowed", old, resizingAllowed); 
    
public voidsetResizingColumn(javax.swing.table.TableColumn aColumn)
Sets the header's resizingColumn to aColumn.

Application code will not use this method explicitly, it is used internally by the column sizing mechanism.

param
aColumn the column being resized, or null if no column is being resized

	resizingColumn = aColumn;
    
public voidsetTable(javax.swing.JTable table)
Sets the table associated with this header.

param
table the new table
beaninfo
bound: true description: The table associated with this header.

 
	JTable old = this.table; 
	this.table = table; 
	firePropertyChange("table", old, table); 
    
public voidsetUI(javax.swing.plaf.TableHeaderUI ui)
Sets the look and feel (L&F) object that renders this component.

param
ui the TableHeaderUI L&F object
see
UIDefaults#getUI

        if (this.ui != ui) {
            super.setUI(ui);
            repaint();
        }
    
public voidsetUpdateTableInRealTime(boolean flag)
Obsolete as of Java 2 platform v1.3. Real time repaints, in response to column dragging or resizing, are now unconditional.

	updateTableInRealTime = flag;
    
public voidupdateUI()
Notification from the UIManager that the look and feel (L&F) has changed. Replaces the current UI object with the latest version from the UIManager.

see
JComponent#updateUI

	setUI((TableHeaderUI)UIManager.getUI(this));

        TableCellRenderer renderer = getDefaultRenderer();
        if (renderer instanceof Component) {
            SwingUtilities.updateComponentTreeUI((Component)renderer);
        }
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject and writeObject in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
	if ((ui != null) && (getUIClassID().equals(uiClassID))) {
	    ui.installUI(this);
	}