* * @param columnModel the new data source for this table * @exception IllegalArgumentException * if <code>newModel</code> is <code>null</code> * @see #getColumnModel * @beaninfo * bound: true * description: The object governing the way columns appear in the view. */ public void setColumnModel(TableColumnModel columnModel) { 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(); } } /** * Returns the <code>TableColumnModel</code> that contains all column information * of this table header. * * @return the <code>columnModel</code> property * @see #setColumnModel */ public TableColumnModel getColumnModel() { return columnModel; } //// Implementing TableColumnModelListener interface// /** * Invoked when a column is added to the table column model. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnAdded(TableColumnModelEvent e) { resizeAndRepaint(); } /** * Invoked when a column is removed from the table column model. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnRemoved(TableColumnModelEvent e) { resizeAndRepaint(); } /** * Invoked when a column is repositioned. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnMoved(TableColumnModelEvent e) { repaint(); } /** * Invoked when a column is moved due to a margin change. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnMarginChanged(ChangeEvent e) { resizeAndRepaint(); } // --Redrawing the header is slow in cell selection mode. // --Since header selection is ugly and it is always clear from the // --view which columns are selected, don't redraw the header. /** * Invoked when the selection model of the <code>TableColumnModel</code> * is changed. This method currently has no effect (the header is not * redrawn). * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnSelectionChanged(ListSelectionEvent e) { } // repaint(); } //// Package Methods// /** * Returns the default column model object which is * a <code>DefaultTableColumnModel</code>. A subclass can override this * method to return a different column model object * * @return the default column model object */ protected TableColumnModel createDefaultColumnModel() { return new DefaultTableColumnModel(); } /** * Returns a default renderer to be used when no header renderer * is defined by a <code>TableColumn</code>. * * @return the default table column renderer * @since 1.3 */ protected TableCellRenderer createDefaultRenderer() { return new DefaultTableCellHeaderRenderer(); } /** * Initializes the local variables and properties with default values. * Used by the constructor methods. */ protected void initializeLocalVars() { 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()); } /** * Sizes the header and marks it as needing display. Equivalent * to <code>revalidate</code> followed by <code>repaint</code>. */ public void resizeAndRepaint() { revalidate(); repaint(); } /** * Sets the header's <code>draggedColumn</code> to <code>aColumn</code>. * <p> * Application code will not use this method explicitly, it is used * internally by the column dragging mechanism. * * @param aColumn the column being dragged, or <code>null</code> if * no column is being dragged */ public void setDraggedColumn(TableColumn aColumn) { draggedColumn = aColumn; } /** * Sets the header's <code>draggedDistance</code> to <code>distance</code>. * @param distance the distance dragged */ public void setDraggedDistance(int distance) { draggedDistance = distance; } /** * Sets the header's <code>resizingColumn</code> to <code>aColumn</code>. * <p> * Application code will not use this method explicitly, it * is used internally by the column sizing mechanism. * * @param aColumn the column being resized, or <code>null</code> if * no column is being resized */ public void setResizingColumn(TableColumn aColumn) { resizingColumn = aColumn; } /** * See <code>readObject</code> and <code>writeObject</code> in * <code>JComponent</code> for more * information about serialization in Swing. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if ((ui != null) && (getUIClassID().equals(uiClassID))) { ui.installUI(this); } } private int getWidthInRightToLeft() { if ((table != null) && (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF)) { return table.getWidth(); } return super.getWidth(); } /** * Returns a string representation of this <code>JTableHeader</code>. 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 <code>null</code>. * <P> * Overriding <code>paramString</code> to provide information about the * specific new aspects of the JFC components. * * @return a string representation of this <code>JTableHeader</code> */ protected String paramString() { 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; } /////////////////// Accessibility support//////////////// /** * 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 */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) { accessibleContext = new AccessibleJTableHeader(); } return accessibleContext; } // // *** should also implement AccessibleSelection? // *** and what's up with keyboard navigation/manipulation? // /** * This class implements accessibility support for the * <code>JTableHeader</code> class. It provides an implementation of the * Java Accessibility API appropriate to table header user-interface * elements. * <p> * <strong>Warning:</strong> * 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 JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ protected class AccessibleJTableHeader extends AccessibleJComponent { /** * Get the role of this object. * * @return an instance of AccessibleRole describing the role of the * object * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.PANEL; } /** * Returns the Accessible child, if one exists, contained at the local * coordinate Point. * * @param p The point defining the top-left corner of the Accessible, * given in the coordinate space of the object's parent. * @return the Accessible, if it exists, at the specified location; * else null */ public Accessible getAccessibleAt(Point p) { int column; // Locate the renderer under the Point if ((column = JTableHeader.this.columnAtPoint(p)) != -1) { TableColumn aColumn = JTableHeader.this.columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component component = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); return new AccessibleJTableHeaderEntry(column, JTableHeader.this, JTableHeader.this.table); } else { return null; } } /** * Returns the number of accessible children in the object. If all * of the children of this object implement Accessible, than this * method should return the number of children of this object. * * @return the number of accessible children in the object. */ public int getAccessibleChildrenCount() { return JTableHeader.this.columnModel.getColumnCount(); } /** * Return the nth Accessible child of the object. * * @param i zero-based index of child * @return the nth Accessible child of the object */ public Accessible getAccessibleChild(int i) { if (i < 0 || i >= getAccessibleChildrenCount()) { return null; } else { TableColumn aColumn = JTableHeader.this.columnModel.getColumn(i); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component component = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, i); return new AccessibleJTableHeaderEntry(i, JTableHeader.this, JTableHeader.this.table); } } /** * This class provides an implementation of the Java Accessibility * API appropropriate for JTableHeader entries. */ protected class AccessibleJTableHeaderEntry extends AccessibleContext implements Accessible, AccessibleComponent { private JTableHeader parent; private int column; private JTable table; /** * Constructs an AccessiblJTableHeaaderEntry * @since 1.4 */ public AccessibleJTableHeaderEntry(int c, JTableHeader p, JTable t) { parent = p; column = c; table = t; this.setAccessibleParent(parent); } /** * Get the AccessibleContext associated with this object. * In the implementation of the Java Accessibility API * for this class, returns this object, which serves as * its own AccessibleContext. * * @return this object */ public AccessibleContext getAccessibleContext() { return this; } private AccessibleContext getCurrentAccessibleContext() { TableColumnModel tcm = table.getColumnModel(); if (tcm != null) { // Fixes 4772355 - ArrayOutOfBoundsException in // JTableHeader if (column < 0 || column >= tcm.getColumnCount()) { return null; } TableColumn aColumn = tcm.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component c = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); if (c instanceof Accessible) { return ((Accessible) c).getAccessibleContext(); } } return null; } private Component getCurrentComponent() { TableColumnModel tcm = table.getColumnModel(); if (tcm != null) { // Fixes 4772355 - ArrayOutOfBoundsException in // JTableHeader if (column < 0 || column >= tcm.getColumnCount()) { return null; } TableColumn aColumn = tcm.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } return renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); } else { return null; } } // AccessibleContext methods public String getAccessibleName() { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { String name = ac.getAccessibleName(); if ((name != null) && (name != "")) { // return the cell renderer's AccessibleName return name; } } if ((accessibleName != null) && (accessibleName != "")) { return accessibleName; } else { // fall back to the client property String name = (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY); if (name != null) { return name; } else { return table.getColumnName(column); } } } public void setAccessibleName(String s) { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { ac.setAccessibleName(s); } else { super.setAccessibleName(s); } } // // *** should check toolTip text for desc. (needs MouseEvent) // public String getAccessibleDescription() { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { return ac.getAccessibleDescription(); } else { return super.getAccessibleDescription(); }Lines 501–1000 of 1472. Previous lines Load more
* * @param columnModel the new data source for this table * @exception IllegalArgumentException * if <code>newModel</code> is <code>null</code> * @see #getColumnModel * @beaninfo * bound: true * description: The object governing the way columns appear in the view. */ public void setColumnModel(TableColumnModel columnModel) { 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(); } } /** * Returns the <code>TableColumnModel</code> that contains all column information * of this table header. * * @return the <code>columnModel</code> property * @see #setColumnModel */ public TableColumnModel getColumnModel() { return columnModel; } // // Implementing TableColumnModelListener interface // /** * Invoked when a column is added to the table column model. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnAdded(TableColumnModelEvent e) { resizeAndRepaint(); } /** * Invoked when a column is removed from the table column model. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnRemoved(TableColumnModelEvent e) { resizeAndRepaint(); } /** * Invoked when a column is repositioned. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnMoved(TableColumnModelEvent e) { repaint(); } /** * Invoked when a column is moved due to a margin change. * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnMarginChanged(ChangeEvent e) { resizeAndRepaint(); } // --Redrawing the header is slow in cell selection mode. // --Since header selection is ugly and it is always clear from the // --view which columns are selected, don't redraw the header. /** * Invoked when the selection model of the <code>TableColumnModel</code> * is changed. This method currently has no effect (the header is not * redrawn). * <p> * Application code will not use these methods explicitly, they * are used internally by <code>JTable</code>. * * @param e the event received * @see TableColumnModelListener */ public void columnSelectionChanged(ListSelectionEvent e) { } // repaint(); } // // Package Methods // /** * Returns the default column model object which is * a <code>DefaultTableColumnModel</code>. A subclass can override this * method to return a different column model object * * @return the default column model object */ protected TableColumnModel createDefaultColumnModel() { return new DefaultTableColumnModel(); } /** * Returns a default renderer to be used when no header renderer * is defined by a <code>TableColumn</code>. * * @return the default table column renderer * @since 1.3 */ protected TableCellRenderer createDefaultRenderer() { return new DefaultTableCellHeaderRenderer(); } /** * Initializes the local variables and properties with default values. * Used by the constructor methods. */ protected void initializeLocalVars() { 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()); } /** * Sizes the header and marks it as needing display. Equivalent * to <code>revalidate</code> followed by <code>repaint</code>. */ public void resizeAndRepaint() { revalidate(); repaint(); } /** * Sets the header's <code>draggedColumn</code> to <code>aColumn</code>. * <p> * Application code will not use this method explicitly, it is used * internally by the column dragging mechanism. * * @param aColumn the column being dragged, or <code>null</code> if * no column is being dragged */ public void setDraggedColumn(TableColumn aColumn) { draggedColumn = aColumn; } /** * Sets the header's <code>draggedDistance</code> to <code>distance</code>. * @param distance the distance dragged */ public void setDraggedDistance(int distance) { draggedDistance = distance; } /** * Sets the header's <code>resizingColumn</code> to <code>aColumn</code>. * <p> * Application code will not use this method explicitly, it * is used internally by the column sizing mechanism. * * @param aColumn the column being resized, or <code>null</code> if * no column is being resized */ public void setResizingColumn(TableColumn aColumn) { resizingColumn = aColumn; } /** * See <code>readObject</code> and <code>writeObject</code> in * <code>JComponent</code> for more * information about serialization in Swing. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if ((ui != null) && (getUIClassID().equals(uiClassID))) { ui.installUI(this); } } private int getWidthInRightToLeft() { if ((table != null) && (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF)) { return table.getWidth(); } return super.getWidth(); } /** * Returns a string representation of this <code>JTableHeader</code>. 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 <code>null</code>. * <P> * Overriding <code>paramString</code> to provide information about the * specific new aspects of the JFC components. * * @return a string representation of this <code>JTableHeader</code> */ protected String paramString() { 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; } ///////////////// // Accessibility support //////////////// /** * 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 */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) { accessibleContext = new AccessibleJTableHeader(); } return accessibleContext; } // // *** should also implement AccessibleSelection? // *** and what's up with keyboard navigation/manipulation? // /** * This class implements accessibility support for the * <code>JTableHeader</code> class. It provides an implementation of the * Java Accessibility API appropriate to table header user-interface * elements. * <p> * <strong>Warning:</strong> * 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 JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ protected class AccessibleJTableHeader extends AccessibleJComponent { /** * Get the role of this object. * * @return an instance of AccessibleRole describing the role of the * object * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.PANEL; } /** * Returns the Accessible child, if one exists, contained at the local * coordinate Point. * * @param p The point defining the top-left corner of the Accessible, * given in the coordinate space of the object's parent. * @return the Accessible, if it exists, at the specified location; * else null */ public Accessible getAccessibleAt(Point p) { int column; // Locate the renderer under the Point if ((column = JTableHeader.this.columnAtPoint(p)) != -1) { TableColumn aColumn = JTableHeader.this.columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component component = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); return new AccessibleJTableHeaderEntry(column, JTableHeader.this, JTableHeader.this.table); } else { return null; } } /** * Returns the number of accessible children in the object. If all * of the children of this object implement Accessible, than this * method should return the number of children of this object. * * @return the number of accessible children in the object. */ public int getAccessibleChildrenCount() { return JTableHeader.this.columnModel.getColumnCount(); } /** * Return the nth Accessible child of the object. * * @param i zero-based index of child * @return the nth Accessible child of the object */ public Accessible getAccessibleChild(int i) { if (i < 0 || i >= getAccessibleChildrenCount()) { return null; } else { TableColumn aColumn = JTableHeader.this.columnModel.getColumn(i) ; TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component component = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, i); return new AccessibleJTableHeaderEntry(i, JTableHeader.this, JTableHeader.this.table); } } /** * This class provides an implementation of the Java Accessibility * API appropropriate for JTableHeader entries. */ protected class AccessibleJTableHeaderEntry extends AccessibleContext implements Accessible, AccessibleComponent { private JTableHeader parent; private int column; private JTable table; /** * Constructs an AccessiblJTableHeaaderEntry * @since 1.4 */ public AccessibleJTableHeaderEntry(int c, JTableHeader p, JTable t) { parent = p; column = c; table = t; this.setAccessibleParent(parent); } /** * Get the AccessibleContext associated with this object. * In the implementation of the Java Accessibility API * for this class, returns this object, which serves as * its own AccessibleContext. * * @return this object */ public AccessibleContext getAccessibleContext() { return this; } private AccessibleContext getCurrentAccessibleContext() { TableColumnModel tcm = table.getColumnModel(); if (tcm != null) { // Fixes 4772355 - ArrayOutOfBoundsException in // JTableHeader if (column < 0 || column >= tcm.getColumnCount()) { return null; } TableColumn aColumn = tcm.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } Component c = renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); if (c instanceof Accessible) { return ((Accessible) c).getAccessibleContext(); } } return null; } private Component getCurrentComponent() { TableColumnModel tcm = table.getColumnModel(); if (tcm != null) { // Fixes 4772355 - ArrayOutOfBoundsException in // JTableHeader if (column < 0 || column >= tcm.getColumnCount()) { return null; } TableColumn aColumn = tcm.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { if (defaultRenderer != null) { renderer = defaultRenderer; } else { return null; } } return renderer.getTableCellRendererComponent( JTableHeader.this.getTable(), aColumn.getHeaderValue(), false, false, -1, column); } else { return null; } } // AccessibleContext methods public String getAccessibleName() { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { String name = ac.getAccessibleName(); if ((name != null) && (name != "")) { // return the cell renderer's AccessibleName return name; } } if ((accessibleName != null) && (accessibleName != "")) { return accessibleName; } else { // fall back to the client property String name = (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY); if (name != null) { return name; } else { return table.getColumnName(column); } } } public void setAccessibleName(String s) { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { ac.setAccessibleName(s); } else { super.setAccessibleName(s); } } // // *** should check toolTip text for desc. (needs MouseEvent) // public String getAccessibleDescription() { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { return ac.getAccessibleDescription(); } else { return super.getAccessibleDescription(); }