📄 jtable.java
字号:
class TableColumnPropertyChangeHandler implements PropertyChangeListener { /** * Receives notification that a property of the observed TableColumns * has changed. * * @param ev the property change event */ public void propertyChange(PropertyChangeEvent ev) { if (ev.getPropertyName().equals("preferredWidth")) { JTableHeader header = getTableHeader(); TableColumn col = (TableColumn) ev.getSource(); header.setResizingColumn(col); doLayout(); header.setResizingColumn(null); } } } /** * A cell renderer for boolean values. */ private class BooleanCellRenderer extends DefaultTableCellRenderer { /** * The CheckBox that is used for rendering. */ private JCheckBox checkBox = new JCheckBox(); /** * Returns the component that is used for rendering the value. * * @param table the JTable * @param value the value of the object * @param isSelected is the cell selected? * @param hasFocus has the cell the focus? * @param row the row to render * @param column the cell to render * * @return this component (the default table cell renderer) */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Boolean boolValue = (Boolean) value; checkBox.setSelected(boolValue.booleanValue()); return checkBox; } } /** * A cell renderer for Date values. */ private class DateCellRenderer extends DefaultTableCellRenderer { /** * Returns the component that is used for rendering the value. * * @param table the JTable * @param value the value of the object * @param isSelected is the cell selected? * @param hasFocus has the cell the focus? * @param row the row to render * @param column the cell to render * * @return this component (the default table cell renderer) */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (value instanceof Date) { Date dateValue = (Date) value; DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); setText(df.format(dateValue)); } return this; } } /** * A cell renderer for Double values. */ private class DoubleCellRenderer extends DefaultTableCellRenderer { /** * Creates a new instance of NumberCellRenderer. */ public DoubleCellRenderer() { setHorizontalAlignment(JLabel.RIGHT); } /** * Returns the component that is used for rendering the value. * * @param table the JTable * @param value the value of the object * @param isSelected is the cell selected? * @param hasFocus has the cell the focus? * @param row the row to render * @param column the cell to render * * @return this component (the default table cell renderer) */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (value instanceof Double) { Double doubleValue = (Double) value; NumberFormat nf = NumberFormat.getInstance(); setText(nf.format(doubleValue.doubleValue())); } return this; } } /** * A cell renderer for Float values. */ private class FloatCellRenderer extends DefaultTableCellRenderer { /** * Creates a new instance of NumberCellRenderer. */ public FloatCellRenderer() { setHorizontalAlignment(JLabel.RIGHT); } /** * Returns the component that is used for rendering the value. * * @param table the JTable * @param value the value of the object * @param isSelected is the cell selected? * @param hasFocus has the cell the focus? * @param row the row to render * @param column the cell to render * * @return this component (the default table cell renderer) */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (value instanceof Float) { Float floatValue = (Float) value; NumberFormat nf = NumberFormat.getInstance(); setText(nf.format(floatValue.floatValue())); } return this; } } /** * A cell renderer for Number values. */ private class NumberCellRenderer extends DefaultTableCellRenderer { /** * Creates a new instance of NumberCellRenderer. */ public NumberCellRenderer() { setHorizontalAlignment(JLabel.RIGHT); } } /** * A cell renderer for Icon values. */ private class IconCellRenderer extends DefaultTableCellRenderer { /** * Returns the component that is used for rendering the value. * * @param table the JTable * @param value the value of the object * @param isSelected is the cell selected? * @param hasFocus has the cell the focus? * @param row the row to render * @param column the cell to render * * @return this component (the default table cell renderer) */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (value instanceof Icon) { Icon iconValue = (Icon) value; setIcon(iconValue); } return this; } } 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(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; /** * @deprecated 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}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -