📄 basictableui.java
字号:
public void installUI(JComponent c) { table = (JTable)c; rendererPane = new CellRendererPane(); table.add(rendererPane); installDefaults(); installDefaults2(); installListeners(); installKeyboardActions(); } /** * Initialize JTable properties, e.g. font, foreground, and background. * The font, foreground, and background properties are only set if their * current value is either null or a UIResource, other properties are set * if the current value is null. * * @see #installUI */ protected void installDefaults() { LookAndFeel.installColorsAndFont(table, "Table.background", "Table.foreground", "Table.font"); // JTable's original row height is 16. To correctly display the // contents on Linux we should have set it to 18, Windows 19 and // Solaris 20. As these values vary so much it's too hard to // be backward compatable and try to update the row height, we're // therefor NOT going to adjust the row height based on font. If the // developer changes the font, it's there responsability to update // the row height. LookAndFeel.installProperty(table, "opaque", Boolean.TRUE); Color sbg = table.getSelectionBackground(); if (sbg == null || sbg instanceof UIResource) { table.setSelectionBackground(UIManager.getColor("Table.selectionBackground")); } Color sfg = table.getSelectionForeground(); if (sfg == null || sfg instanceof UIResource) { table.setSelectionForeground(UIManager.getColor("Table.selectionForeground")); } Color gridColor = table.getGridColor(); if (gridColor == null || gridColor instanceof UIResource) { table.setGridColor(UIManager.getColor("Table.gridColor")); } // install the scrollpane border Container parent = table.getParent(); // should be viewport if (parent != null) { parent = parent.getParent(); // should be the scrollpane if (parent != null && parent instanceof JScrollPane) { LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder"); } } isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList")); } private void installDefaults2() { TransferHandler th = table.getTransferHandler(); if (th == null || th instanceof UIResource) { table.setTransferHandler(defaultTransferHandler); } } /** * Attaches listeners to the JTable. */ protected void installListeners() { focusListener = createFocusListener(); keyListener = createKeyListener(); mouseInputListener = createMouseInputListener(); table.addFocusListener(focusListener); table.addKeyListener(keyListener); table.addMouseListener(mouseInputListener); table.addMouseMotionListener(mouseInputListener); table.addPropertyChangeListener(getHandler()); if (isFileList) { table.getSelectionModel().addListSelectionListener(getHandler()); } } /** * Register all keyboard actions on the JTable. */ protected void installKeyboardActions() { LazyActionMap.installLazyActionMap(table, BasicTableUI.class, "Table.actionMap"); InputMap inputMap = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(table, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { InputMap keyMap = (InputMap)DefaultLookup.get(table, this, "Table.ancestorInputMap"); return keyMap; } return null; } static void loadActionMap(LazyActionMap map) { // IMPORTANT: There is a very close coupling between the parameters // passed to the Actions constructor. Only certain parameter // combinations are supported. For example, the following Action would // not work as expected: // new Actions(Actions.NEXT_ROW_CELL, 1, 4, false, true) // Actions which move within the selection only (having a true // inSelection parameter) require that one of dx or dy be // zero and the other be -1 or 1. The point of this warning is // that you should be very careful about making sure a particular // combination of parameters is supported before changing or // adding anything here. map.put(new Actions(Actions.NEXT_COLUMN, 1, 0, false, false)); map.put(new Actions(Actions.NEXT_COLUMN_CHANGE_LEAD, 1, 0, false, false)); map.put(new Actions(Actions.PREVIOUS_COLUMN, -1, 0, false, false)); map.put(new Actions(Actions.PREVIOUS_COLUMN_CHANGE_LEAD, -1, 0, false, false)); map.put(new Actions(Actions.NEXT_ROW, 0, 1, false, false)); map.put(new Actions(Actions.NEXT_ROW_CHANGE_LEAD, 0, 1, false, false)); map.put(new Actions(Actions.PREVIOUS_ROW, 0, -1, false, false)); map.put(new Actions(Actions.PREVIOUS_ROW_CHANGE_LEAD, 0, -1, false, false)); map.put(new Actions(Actions.NEXT_COLUMN_EXTEND_SELECTION, 1, 0, true, false)); map.put(new Actions(Actions.PREVIOUS_COLUMN_EXTEND_SELECTION, -1, 0, true, false)); map.put(new Actions(Actions.NEXT_ROW_EXTEND_SELECTION, 0, 1, true, false)); map.put(new Actions(Actions.PREVIOUS_ROW_EXTEND_SELECTION, 0, -1, true, false)); map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION, false, false, true, false)); map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION, false, true, true, false)); map.put(new Actions(Actions.FIRST_COLUMN, false, false, false, true)); map.put(new Actions(Actions.LAST_COLUMN, false, true, false, true)); map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION, true, false, true, false)); map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION, true, true, true, false)); map.put(new Actions(Actions.FIRST_COLUMN_EXTEND_SELECTION, true, false, false, true)); map.put(new Actions(Actions.LAST_COLUMN_EXTEND_SELECTION, true, true, false, true)); map.put(new Actions(Actions.FIRST_ROW, false, false, true, true)); map.put(new Actions(Actions.LAST_ROW, false, true, true, true)); map.put(new Actions(Actions.FIRST_ROW_EXTEND_SELECTION, true, false, true, true)); map.put(new Actions(Actions.LAST_ROW_EXTEND_SELECTION, true, true, true, true)); map.put(new Actions(Actions.NEXT_COLUMN_CELL, 1, 0, false, true)); map.put(new Actions(Actions.PREVIOUS_COLUMN_CELL, -1, 0, false, true)); map.put(new Actions(Actions.NEXT_ROW_CELL, 0, 1, false, true)); map.put(new Actions(Actions.PREVIOUS_ROW_CELL, 0, -1, false, true)); map.put(new Actions(Actions.SELECT_ALL)); map.put(new Actions(Actions.CLEAR_SELECTION)); map.put(new Actions(Actions.CANCEL_EDITING)); map.put(new Actions(Actions.START_EDITING)); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_SELECTION, false, false, false, false)); map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_SELECTION, false, true, false, false)); map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION, true, false, false, false)); map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION, true, true, false, false)); map.put(new Actions(Actions.ADD_TO_SELECTION)); map.put(new Actions(Actions.TOGGLE_AND_ANCHOR)); map.put(new Actions(Actions.EXTEND_TO)); map.put(new Actions(Actions.MOVE_SELECTION_TO)); map.put(new Actions(Actions.FOCUS_HEADER)); }// Uninstallation public void uninstallUI(JComponent c) { uninstallDefaults(); uninstallListeners(); uninstallKeyboardActions(); table.remove(rendererPane); rendererPane = null; table = null; } protected void uninstallDefaults() { if (table.getTransferHandler() instanceof UIResource) { table.setTransferHandler(null); } } protected void uninstallListeners() { table.removeFocusListener(focusListener); table.removeKeyListener(keyListener); table.removeMouseListener(mouseInputListener); table.removeMouseMotionListener(mouseInputListener); table.removePropertyChangeListener(getHandler()); if (isFileList) { table.getSelectionModel().removeListSelectionListener(getHandler()); } focusListener = null; keyListener = null; mouseInputListener = null; handler = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIInputMap(table, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIActionMap(table, null); } /** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults(); Component renderer = (Component)lafDefaults.get( BASELINE_COMPONENT_KEY); if (renderer == null) { DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(); renderer = tcr.getTableCellRendererComponent( table, "a", false, false, -1, -1); lafDefaults.put(BASELINE_COMPONENT_KEY, renderer); } renderer.setFont(table.getFont()); int rowMargin = table.getRowMargin(); return renderer.getBaseline(Integer.MAX_VALUE, table.getRowHeight() - rowMargin) + rowMargin / 2; } /** * Returns an enum indicating how the baseline of the component * changes as the size changes. * * @throws NullPointerException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public Component.BaselineResizeBehavior getBaselineResizeBehavior( JComponent c) { super.getBaselineResizeBehavior(c); return Component.BaselineResizeBehavior.CONSTANT_ASCENT; }//// Size Methods// private Dimension createTableSize(long width) { int height = 0; int rowCount = table.getRowCount(); if (rowCount > 0 && table.getColumnCount() > 0) { Rectangle r = table.getCellRect(rowCount-1, 0, true); height = r.y + r.height; } // Width is always positive. The call to abs() is a workaround for // a bug in the 1.1.6 JIT on Windows. long tmp = Math.abs(width); if (tmp > Integer.MAX_VALUE) { tmp = Integer.MAX_VALUE; } return new Dimension((int)tmp, height); } /** * Return the minimum size of the table. The minimum height is the * row height times the number of rows. * The minimum width is the sum of the minimum widths of each column. */ public Dimension getMinimumSize(JComponent c) { long width = 0; Enumeration enumeration = table.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn)enumeration.nextElement(); width = width + aColumn.getMinWidth(); } return createTableSize(width); } /** * Return the preferred size of the table. The preferred height is the * row height times the number of rows. * The preferred width is the sum of the preferred widths of each column. */ public Dimension getPreferredSize(JComponent c) { long width = 0; Enumeration enumeration = table.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn)enumeration.nextElement(); width = width + aColumn.getPreferredWidth(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -