?? jtable.java
字號:
/** * 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(TableModel) * @see #createDefaultColumnsFromModel() * @see #setColumnModel(TableColumnModel) * @see #setAutoCreateColumnsFromModel(boolean) * @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(int) * @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(int) * @see TableColumn#getWidth() * @see TableColumn#setWidth(int) */ protected int rowHeight; /** * The height in pixels of the gap left between any two rows of the table. * * @see #setRowMargin(int) * @see #getRowHeight() * @see #getIntercellSpacing() * @see #setIntercellSpacing(Dimension) * @see TableColumnModel#getColumnMargin() * @see TableColumnModel#setColumnMargin(int) */ 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#getColumnSelectionAllowed() * @see #setRowSelectionAllowed(boolean) * @see #getRowSelectionAllowed() * @see #getCellSelectionEnabled() * @see #setCellSelectionEnabled(boolean) */ protected boolean rowSelectionAllowed; /** * Obsolete. Use {@link #rowSelectionAllowed}, {@link * #getColumnSelectionAllowed}, or the combined methods {@link * #getCellSelectionEnabled} and {@link #setCellSelectionEnabled(boolean)}. */ 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(TableModelEvent) * @see TableModel#addTableModelListener(TableModelListener) */ 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(TableColumnModel) * @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(ListSelectionModel) * @see #getSelectionModel() * @see TableColumnModel#getSelectionModel() * @see ListSelectionModel#addListSelectionListener(ListSelectionListener) */ protected ListSelectionModel selectionModel; /** * The current cell editor. */ protected TableCellEditor cellEditor; /** * Whether or not drag-and-drop is enabled on this table. * * @see #setDragEnabled(boolean) * @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(Color) * @see #getGridColor() */ protected Color gridColor; /** * The size this table would prefer its viewport assume, if it is * contained in a {@link JScrollPane}. * * @see #setPreferredScrollableViewportSize(Dimension) * @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(Color) * @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(Color) * @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; /** * The property handler for this table's columns. */ TableColumnPropertyChangeHandler tableColumnPropertyChangeHandler = new TableColumnPropertyChangeHandler(); /** * Whether cell editors should receive keyboard focus when the table is * activated. */ private boolean surrendersFocusOnKeystroke = false; /** * A Rectangle object to be reused in {@link #getCellRect}. */ private Rectangle rectCache = new Rectangle(); /** * Creates a new <code>JTable</code> instance. */ public JTable () { this(null, null, null); } /** * Creates a new <code>JTable</code> instance with the given number * of rows and columns. * * @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, storing the given data * array and heaving the given column names. To see the column names, * you must place the JTable into the {@link JScrollPane}. * * @param data an <code>Object[][]</code> the table data * @param columnNames an <code>Object[]</code> the column headers */ public JTable(Object[][] data, Object[] columnNames) { this(new DefaultTableModel(data, columnNames)); } /** * Creates a new <code>JTable</code> instance, using the given data model * object that provides information about the table content. The table model * object is asked for the table size, other features and also receives * notifications in the case when the table has been edited by the user. * * @param model * the table model. */ public JTable (TableModel model) { this(model, null, null); } /** * Creates a new <code>JTable</code> instance, using the given model object * that provides information about the table content. The table data model * object is asked for the table size, other features and also receives * notifications in the case when the table has been edited by the user. The * table column model provides more detailed control on the table column * related features. * * @param dm * the table data mode * @param cm * the table column model */ public JTable (TableModel dm, TableColumnModel cm) { this(dm, cm, null); } /** * Creates a new <code>JTable</code> instance, providing data model, * column model and list selection model. The list selection model * manages the selections. * * @param dm data model (manages table data) * @param cm column model (manages table columns) * @param sm list selection model (manages table selections) */ public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm) { boolean autoCreate = false; if (cm != null) setColumnModel(cm); else { setColumnModel(createDefaultColumnModel()); autoCreate = true; } setSelectionModel(sm == null ? createDefaultSelectionModel() : sm); setModel(dm == null ? createDefaultDataModel() : dm); setAutoCreateColumnsFromModel(autoCreate); initializeLocalVars(); // The following four lines properly set the lead selection indices. // After this, the UI will handle the lead selection indices. // FIXME: this should probably not be necessary, if the UI is installed // before the TableModel is set then the UI will handle things on its // own, but certain variables need to be set before the UI can be installed // so we must get the correct order for all the method calls in this // constructor. selectionModel.setAnchorSelectionIndex(0); selectionModel.setLeadSelectionIndex(0); columnModel.getSelectionModel().setAnchorSelectionIndex(0); columnModel.getSelectionModel().setLeadSelectionIndex(0); updateUI(); } /** * Creates a new <code>JTable</code> instance that uses data and column * names, stored in {@link Vector}s. * * @param data the table data * @param columnNames the table column names. */ public JTable(Vector data, Vector columnNames) { this(new DefaultTableModel(data, columnNames)); } /** * Initialize local variables to default values. */ protected void initializeLocalVars() { setTableHeader(createDefaultTableHeader()); if (autoCreateColumnsFromModel) createDefaultColumnsFromModel(); this.columnModel.addColumnModelListener(this); this.defaultRenderersByColumnClass = new Hashtable(); createDefaultRenderers(); this.defaultEditorsByColumnClass = new Hashtable(); createDefaultEditors(); this.autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS; this.rowHeight = 16; this.rowMargin = 1; this.rowSelectionAllowed = true; // this.accessibleContext = new AccessibleJTable(); this.cellEditor = null; // COMPAT: Both Sun and IBM have drag enabled this.dragEnabled = true; this.preferredViewportSize = new Dimension(450,400); this.showHorizontalLines = true; this.showVerticalLines = true; this.editingColumn = -1; this.editingRow = -1; setIntercellSpacing(new Dimension(1,1)); } /** * Add the new table column. The table column class allows to specify column * features more precisely, setting the preferred width, column data type * (column class) and table headers. * * There is no need the add columns to the table if the default column * handling is sufficient. * * @param column * the new column to add. */ public void addColumn(TableColumn column) { if (column.getHeaderValue() == null) { String name = dataModel.getColumnName(column.getModelIndex()); column.setHeaderValue(name); } columnModel.addColumn(column); column.addPropertyChangeListener(tableColumnPropertyChangeHandler); } /** * Create the default editors for this table. The default method creates * the editor for Booleans. * * Other fields are edited as strings at the moment. */ protected void createDefaultEditors() { JCheckBox box = new BooleanCellRenderer().getCheckBox(); setDefaultEditor(Boolean.class, new DefaultCellEditor(box)); } /** * Create the default renderers for this table. The default method creates * renderers for Boolean, Number, Double, Date, Icon and ImageIcon. * */ protected void createDefaultRenderers() { setDefaultRenderer(Boolean.class, new BooleanCellRenderer()); setDefaultRenderer(Number.class, new NumberCellRenderer()); setDefaultRenderer(Double.class, new DoubleCellRenderer()); setDefaultRenderer(Double.class, new FloatCellRenderer()); setDefaultRenderer(Date.class, new DateCellRenderer()); setDefaultRenderer(Icon.class, new IconCellRenderer()); setDefaultRenderer(ImageIcon.class, new IconCellRenderer()); } /** * @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code> */ public static JScrollPane createScrollPaneForTable(JTable table) { return new JScrollPane(table); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -