📄 basictableui.java
字号:
public void mouseClicked(MouseEvent e) {} private void setDispatchComponent(MouseEvent e) { Component editorComponent = table.getEditorComponent(); Point p = e.getPoint(); Point p2 = SwingUtilities.convertPoint(table, p, editorComponent); dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, p2.x, p2.y); } private boolean repostEvent(MouseEvent e) { // Check for isEditing() in case another event has // caused the editor to be removed. See bug #4306499. if (dispatchComponent == null || !table.isEditing()) { return false; } MouseEvent e2 = SwingUtilities.convertMouseEvent(table, e, dispatchComponent); dispatchComponent.dispatchEvent(e2); return true; } private void setValueIsAdjusting(boolean flag) { table.getSelectionModel().setValueIsAdjusting(flag); table.getColumnModel().getSelectionModel().setValueIsAdjusting(flag); } private boolean shouldIgnore(MouseEvent e) { return e.isConsumed() || (!(SwingUtilities.isLeftMouseButton(e) && table.isEnabled())); } public void mousePressed(MouseEvent e) { if (e.isConsumed()) { selectedOnPress = false; return; } selectedOnPress = true; adjustFocusAndSelection(e); } void adjustFocusAndSelection(MouseEvent e) { if (shouldIgnore(e)) { return; } Point p = e.getPoint(); int row = table.rowAtPoint(p); int column = table.columnAtPoint(p); // The autoscroller can generate drag events outside the Table's range. if ((column == -1) || (row == -1)) { return; } if (table.editCellAt(row, column, e)) { setDispatchComponent(e); repostEvent(e); } else if (table.isRequestFocusEnabled()) { table.requestFocus(); } CellEditor editor = table.getCellEditor(); if (editor == null || editor.shouldSelectCell(e)) { boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ? true : false; setValueIsAdjusting(adjusting); table.changeSelection(row, column, e.isControlDown(), e.isShiftDown()); } } public void mouseReleased(MouseEvent e) { if (selectedOnPress) { if (shouldIgnore(e)) { return; } repostEvent(e); dispatchComponent = null; setValueIsAdjusting(false); } else { adjustFocusAndSelection(e); } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {}// The Table's mouse motion listener methods. public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { if (shouldIgnore(e)) { return; } repostEvent(e); CellEditor editor = table.getCellEditor(); if (editor == null || editor.shouldSelectCell(e)) { Point p = e.getPoint(); int row = table.rowAtPoint(p); int column = table.columnAtPoint(p); // The autoscroller can generate drag events outside the Table's range. if ((column == -1) || (row == -1)) { return; } table.changeSelection(row, column, false, true); } } }//// Factory methods for the Listeners// /** * Creates the key listener for handling keyboard navigation in the JTable. */ protected KeyListener createKeyListener() { return null; } /** * Creates the focus listener for handling keyboard navigation in the JTable. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * Creates the mouse listener for the JTable. */ protected MouseInputListener createMouseInputListener() { return new MouseInputHandler(); } /** * Creates the property change listener for the JTable. */ private PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); }//// The installation/uninstall procedures and support// public static ComponentUI createUI(JComponent c) { return new BasicTableUI(); }// Installation public void installUI(JComponent c) { table = (JTable)c; rendererPane = new CellRendererPane(); table.add(rendererPane); installDefaults(); 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"); 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"); } } TransferHandler th = table.getTransferHandler(); if (th == null || th instanceof UIResource) { table.setTransferHandler(defaultTransferHandler); } DropTarget dropTarget = table.getDropTarget(); if (dropTarget instanceof UIResource) { if (defaultDropTargetListener == null) { defaultDropTargetListener = new TableDropTargetListener(); } try { dropTarget.addDropTargetListener(defaultDropTargetListener); } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } } } /** * Attaches listeners to the JTable. */ protected void installListeners() { focusListener = createFocusListener(); keyListener = createKeyListener(); mouseInputListener = createMouseInputListener(); propertyChangeListener = createPropertyChangeListener(); table.addFocusListener(focusListener); table.addKeyListener(keyListener); table.addMouseListener(defaultDragRecognizer); table.addMouseMotionListener(defaultDragRecognizer); table.addMouseListener(mouseInputListener); table.addMouseMotionListener(mouseInputListener); table.addPropertyChangeListener(propertyChangeListener); } /** * Register all keyboard actions on the JTable. */ protected void installKeyboardActions() { ActionMap map = getActionMap(); SwingUtilities.replaceUIActionMap(table, map); 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)UIManager.get("Table.ancestorInputMap"); InputMap rtlKeyMap; if (table.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)UIManager.get("Table.ancestorInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } ActionMap getActionMap() { ActionMap map = (ActionMap)UIManager.get("Table.actionMap"); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put("Table.actionMap", map); } } return map; } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("selectNextColumn", new NavigationalAction (1, 0, false, false, false)); map.put("selectPreviousColumn", new NavigationalAction (-1, 0, false, false, false)); map.put("selectNextRow", new NavigationalAction (0, 1, false, false, false)); map.put("selectPreviousRow", new NavigationalAction (0, -1, false, false, false)); map.put("selectNextColumnExtendSelection", new NavigationalAction (1, 0, false, true, false)); map.put("selectPreviousColumnExtendSelection", new NavigationalAction (-1, 0, false, true, false)); map.put("selectNextRowExtendSelection", new NavigationalAction (0, 1, false, true, false)); map.put("selectPreviousRowExtendSelection", new NavigationalAction (0, -1, false, true, false)); map.put("scrollUpChangeSelection", new PagingAction(false, false, true, false)); map.put("scrollDownChangeSelection", new PagingAction(false, true, true, false)); map.put("selectFirstColumn", new PagingAction(false, false, false, true)); map.put("selectLastColumn", new PagingAction(false, true, false, true)); map.put("scrollUpExtendSelection", new PagingAction(true, false, true, false)); map.put("scrollDownExtendSelection", new PagingAction(true, true, true, false)); map.put("selectFirstColumnExtendSelection", new PagingAction(true, false, false, true)); map.put("selectLastColumnExtendSelection", new PagingAction(true, true, false, true)); map.put("selectFirstRow", new PagingAction(false, false, true, true)); map.put("selectLastRow", new PagingAction(false, true, true, true)); map.put("selectFirstRowExtendSelection", new PagingAction(true, false, true, true)); map.put("selectLastRowExtendSelection", new PagingAction(true, true, true, true)); map.put("selectNextColumnCell", new NavigationalAction(1, 0, true, false, true)); map.put("selectPreviousColumnCell", new NavigationalAction(-1, 0, true, false, true)); map.put("selectNextRowCell", new NavigationalAction(0, 1, true, false, true)); map.put("selectPreviousRowCell", new NavigationalAction(0, -1, true, false, true)); map.put("selectAll", new SelectAllAction()); map.put("cancel", new CancelEditingAction()); map.put("startEditing", new StartEditingAction()); 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()); if (table.getComponentOrientation().isLeftToRight()) { map.put("scrollLeftChangeSelection", new PagingAction(false, false, false, false)); map.put("scrollRightChangeSelection", new PagingAction(false, true, false, false)); map.put("scrollLeftExtendSelection", new PagingAction(true, false, false, false)); map.put("scrollRightExtendSelection", new PagingAction(true, true, false, false)); } else { map.put("scrollLeftChangeSelection", new PagingAction(false, true, false, false)); map.put("scrollRightChangeSelection", new PagingAction(false, false, false, false)); map.put("scrollLeftExtendSelection", new PagingAction(true, true, false, false)); map.put("scrollRightExtendSelection", new PagingAction(true, false, false, false)); } return map; }// 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(defaultDragRecognizer); table.removeMouseMotionListener(defaultDragRecognizer); table.removeMouseListener(mouseInputListener); table.removeMouseMotionListener(mouseInputListener); table.removePropertyChangeListener(propertyChangeListener); focusListener = null; keyListener = null; mouseInputListener = null; propertyChangeListener = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIInputMap(table, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIActionMap(table, null); }//// 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(); } return createTableSize(width); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -