📄 jtable.java
字号:
/* JTable.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Point;import java.awt.Rectangle;import java.util.Hashtable;import java.util.Vector;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.swing.event.CellEditorListener;import javax.swing.event.ChangeEvent;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.TableColumnModelEvent;import javax.swing.event.TableColumnModelListener;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;import javax.swing.plaf.TableUI;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableColumnModel;import javax.swing.table.DefaultTableModel;import javax.swing.table.JTableHeader;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;public class JTable extends JComponent implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible{ private static final long serialVersionUID = 3876025080382781659L; /** * When resizing columns, do not automatically change any columns. In this * case the table should be enclosed in a {@link JScrollPane} in order to * accomodate cases in which the table size exceeds its visible area. */ public static final int AUTO_RESIZE_OFF = 0; /** * When resizing column <code>i</code>, automatically change only the * single column <code>i+1</code> to provide or absorb excess space * requirements. */ public static final int AUTO_RESIZE_NEXT_COLUMN = 1; /** * When resizing column <code>i</code> in a table of <code>n</code> * columns, automatically change all columns in the range <code>[i+1, * n)</code>, uniformly, to provide or absorb excess space requirements. */ public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2; /** * When resizing column <code>i</code> in a table of <code>n</code> * columns, automatically change all columns in the range <code>[0, * n)</code> (with the exception of column i) uniformly, to provide or * absorb excess space requirements. */ public static final int AUTO_RESIZE_ALL_COLUMNS = 4; /** * When resizing column <code>i</code> in a table of <code>n</code> * columns, automatically change column <code>n-1</code> (the last column * in the table) to provide or absorb excess space requirements. */ public static final int AUTO_RESIZE_LAST_COLUMN = 3; /** * A table mapping {@link java.lang.Class} objects to * {@link TableCellEditor} objects. This table is consulted by the * FIXME */ protected Hashtable defaultEditorsByColumnClass; /** * A table mapping {@link java.lang.Class} objects to * {@link TableCellEditor} objects. This table is consulted by the * FIXME */ protected Hashtable defaultRenderersByColumnClass; /** * The column that is edited, -1 if the table is not edited currently. */ protected int editingColumn; /** * The row that is edited, -1 if the table is not edited currently. */ protected int editingRow; /** * The component that is used for editing. * <code>null</code> if the table is not editing currently. * */ protected transient Component editorComp; /** * Whether or not the table should automatically compute a matching * {@link TableColumnModel} and assign it to the {@link #columnModel} * property when the {@link #dataModel} property is changed. * * @see #setModel() * @see #createColumnsFromModel() * @see #setColumnModel() * @see #setAutoCreateColumnsFromModel() * @see #getAutoCreateColumnsFromModel() */ protected boolean autoCreateColumnsFromModel; /** * A numeric code specifying the resizing behavior of the table. Must be * one of {@link #AUTO_RESIZE_ALL_COLUMNS} (the default), {@link * #AUTO_RESIZE_LAST_COLUMN}, {@link #AUTO_RESIZE_NEXT_COLUMN}, {@link * #AUTO_RESIZE_SUBSEQUENT_COLUMNS}, or {@link #AUTO_RESIZE_OFF}. * * @see #doLayout() * @see #setAutoResizeMode() * @see #getAutoResizeMode() */ protected int autoResizeMode; /** * The height in pixels of any row of the table. All rows in a table are * of uniform height. This differs from column width, which varies on a * per-column basis, and is stored in the individual columns of the * {@link #columnModel}. * * @see #getRowHeight() * @see #setRowHeight() * @see TableColumn#getWidth() * @see TableColumn#setWidth() */ protected int rowHeight; /** * The height in pixels of the gap left between any two rows of the table. * * @see #setRowMargin() * @see #getRowHeight() * @see #getIntercellSpacing() * @see #setIntercellSpacing() * @see TableColumnModel#getColumnMargin() * @see TableColumnModel#setColumnMargin() */ protected int rowMargin; /** * Whether or not the table should allow row selection. If the table * allows both row <em>and</em> column selection, it is said to allow * "cell selection". Previous versions of the JDK supported cell * selection as an independent concept, but it is now represented solely * in terms of simultaneous row and column selection. * * @see TableColumnModel#columnSelectionAllowed() * @see #setRowSelectionAllowed() * @see #getRowSelectionAllowed() * @see #getCellSelectionEnabled() * @see #setCellSelectionEnabled() */ protected boolean rowSelectionAllowed; /** * @deprecated Use {@link #rowSelectionAllowed}, {@link * #columnSelectionAllowed}, or the combined methods {@link * getCellSelectionEnabled} and {@link setCellSelectionEnabled}. */ protected boolean cellSelectionEnabled; /** * The model for data stored in the table. Confusingly, the published API * requires that this field be called <code>dataModel</code>, despite its * property name. The table listens to its model as a {@link * TableModelListener}. * * @see #tableChanged() * @see TableModel#addTableModelListener() */ protected TableModel dataModel; /** * <p>A model of various aspects of the columns of the table, <em>not * including</em> the data stored in them. The {@link TableColumnModel} * is principally concerned with holding a set of {@link TableColumn} * objects, each of which describes the display parameters of a column * and the numeric index of the column from the data model which the * column is presenting.</p> * * <p>The TableColumnModel also contains a {@link ListSelectionModel} which * indicates which columns are currently selected. This selection model * works in combination with the {@link selectionModel} of the table * itself to specify a <em>table selection</em>: a combination of row and * column selections.</p> * * <p>Most application programmers do not need to work with this property * at all: setting {@link #autoCreateColumnsFromModel} will construct the * columnModel automatically, and the table acts as a facade for most of * the interesting properties of the columnModel anyways.</p> * * @see #setColumnModel() * @see #getColumnModel() */ protected TableColumnModel columnModel; /** * A model of the rows of this table which are currently selected. This * model is used in combination with the column selection model held as a * member of the {@link columnModel} property, to represent the rows and * columns (or both: cells) of the table which are currently selected. * * @see #rowSelectionAllowed * @see #setSelectionModel() * @see #getSelectionModel() * @see TableColumnModel#getSelectionModel() * @see ListSelectionModel#addListSelectionListener() */ protected ListSelectionModel selectionModel; /** * The accessibleContext property. */ protected AccessibleContext accessibleContext; /** * The current cell editor. */ protected TableCellEditor cellEditor; /** * Whether or not drag-and-drop is enabled on this table. * * @see #setDragEnabled() * @see #getDragEnabled() */ private boolean dragEnabled; /** * The color to paint the grid lines of the table, when either {@link * #showHorizontalLines} or {@link #showVerticalLines} is set. * * @see #setGridColor() * @see #getGridColor() */ protected Color gridColor; /** * The size this table would prefer its viewport assume, if it is * contained in a {@link JScrollPane}. * * @see #setPreferredScrollableViewportSize() * @see #getPreferredScrollableViewportSize() */ protected Dimension preferredViewportSize; /** * The color to paint the background of selected cells. Fires a property * change event with name {@link #SELECTION_BACKGROUND_CHANGED_PROPERTY} * when its value changes. * * @see #setSelectionBackground() * @see #getSelectionBackground() */ protected Color selectionBackground; /** * The name carried in property change events when the {@link * #selectionBackground} property changes. */ private static final String SELECTION_BACKGROUND_CHANGED_PROPERTY = "selectionBackground"; /** * The color to paint the foreground of selected cells. Fires a property * change event with name {@link #SELECTION_FOREGROUND_CHANGED_PROPERTY} * when its value changes. * * @see #setSelectionForeground() * @see #getSelectionForeground() */ protected Color selectionForeground; /** * The name carried in property change events when the * {@link #selectionForeground} property changes. */ private static final String SELECTION_FOREGROUND_CHANGED_PROPERTY = "selectionForeground"; /** * The showHorizontalLines property. */ protected boolean showHorizontalLines; /** * The showVerticalLines property. */ protected boolean showVerticalLines; /** * The tableHeader property. */ protected JTableHeader tableHeader; /** * Creates a new <code>JTable</code> instance. */ public JTable () { this(null, null, null); } /** * Creates a new <code>JTable</code> instance. * * @param numRows an <code>int</code> value * @param numColumns an <code>int</code> value */ public JTable (int numRows, int numColumns) { this(new DefaultTableModel(numRows, numColumns)); } /** * Creates a new <code>JTable</code> instance. * * @param data an <code>Object[][]</code> value * @param columnNames an <code>Object[]</code> value */ public JTable(Object[][] data, Object[] columnNames) { this(new DefaultTableModel(data, columnNames)); } /** * Creates a new <code>JTable</code> instance. * * @param dm a <code>TableModel</code> value */ public JTable (TableModel dm) { this(dm, null, null); } /** * Creates a new <code>JTable</code> instance. * * @param dm a <code>TableModel</code> value * @param cm a <code>TableColumnModel</code> value */ public JTable (TableModel dm, TableColumnModel cm) { this(dm, cm, null); } /** * Creates a new <code>JTable</code> instance. * * @param dm a <code>TableModel</code> value * @param cm a <code>TableColumnModel</code> value * @param sm a <code>ListSelectionModel</code> value */ public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) { this.dataModel = dm == null ? createDefaultDataModel() : dm; setSelectionModel(sm == null ? createDefaultSelectionModel() : sm); this.columnModel = cm; initializeLocalVars(); updateUI(); } protected void initializeLocalVars() { this.autoCreateColumnsFromModel = false; if (columnModel == null) { this.autoCreateColumnsFromModel = true; createColumnsFromModel(); } this.columnModel.addColumnModelListener(this); this.defaultRenderersByColumnClass = new Hashtable(); createDefaultRenderers(); this.defaultEditorsByColumnClass = new Hashtable(); createDefaultEditors(); this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS; this.rowHeight = 16; this.rowMargin = 1; this.rowSelectionAllowed = true; // this.accessibleContext = new AccessibleJTable(); this.cellEditor = null; this.dragEnabled = false; this.preferredViewportSize = new Dimension(450,400); this.showHorizontalLines = true; this.showVerticalLines = true; this.editingColumn = -1; this.editingRow = -1; setIntercellSpacing(new Dimension(1,1)); setTableHeader(createDefaultTableHeader()); } /** * Creates a new <code>JTable</code> instance. * * @param data a <code>Vector</code> value * @param columnNames a <code>Vector</code> value */ public JTable(Vector data, Vector columnNames) { this(new DefaultTableModel(data, columnNames)); } public void addColumn(TableColumn column) { if (column.getHeaderValue() == null) { String name = getColumnName(column.getModelIndex()); column.setHeaderValue(name); } columnModel.addColumn(column); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -