📄 basictableui.java
字号:
} }//// The Table's Key listener// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. * <p>As of Java 2 platform v1.3 this class is no longer used. * Instead <code>JTable</code> * overrides <code>processKeyBinding</code> to dispatch the event to * the current <code>TableCellEditor</code>. */ public class KeyHandler implements KeyListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void keyPressed(KeyEvent e) { getHandler().keyPressed(e); } public void keyReleased(KeyEvent e) { getHandler().keyReleased(e); } public void keyTyped(KeyEvent e) { getHandler().keyTyped(e); } }//// The Table's focus listener// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. */ public class FocusHandler implements FocusListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void focusGained(FocusEvent e) { getHandler().focusGained(e); } public void focusLost(FocusEvent e) { getHandler().focusLost(e); } }//// The Table's mouse and mouse motion listeners// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. */ public class MouseInputHandler implements MouseInputListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void mouseClicked(MouseEvent e) { getHandler().mouseClicked(e); } public void mousePressed(MouseEvent e) { getHandler().mousePressed(e); } public void mouseReleased(MouseEvent e) { getHandler().mouseReleased(e); } public void mouseEntered(MouseEvent e) { getHandler().mouseEntered(e); } public void mouseExited(MouseEvent e) { getHandler().mouseExited(e); } public void mouseMoved(MouseEvent e) { getHandler().mouseMoved(e); } public void mouseDragged(MouseEvent e) { getHandler().mouseDragged(e); } } private class Handler implements FocusListener, MouseInputListener, PropertyChangeListener { // FocusListener private void repaintLeadCell( ) { int lr = getAdjustedLead(table, true); int lc = getAdjustedLead(table, false); if (lr < 0 || lc < 0) { return; } Rectangle dirtyRect = table.getCellRect(lr, lc, false); table.repaint(dirtyRect); } public void focusGained(FocusEvent e) { repaintLeadCell(); } public void focusLost(FocusEvent e) { repaintLeadCell(); } // KeyListener public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyChar(), e.getModifiers()); // We register all actions using ANCESTOR_OF_FOCUSED_COMPONENT // which means that we might perform the appropriate action // in the table and then forward it to the editor if the editor // had focus. Make sure this doesn't happen by checking our // InputMaps. InputMap map = table.getInputMap(JComponent.WHEN_FOCUSED); if (map != null && map.get(keyStroke) != null) { return; } map = table.getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); if (map != null && map.get(keyStroke) != null) { return; } keyStroke = KeyStroke.getKeyStrokeForEvent(e); // The AWT seems to generate an unconsumed \r event when // ENTER (\n) is pressed. if (e.getKeyChar() == '\r') { return; } int leadRow = getAdjustedLead(table, true); int leadColumn = getAdjustedLead(table, false); if (leadRow != -1 && leadColumn != -1 && !table.isEditing()) { if (!table.editCellAt(leadRow, leadColumn)) { return; } } // Forwarding events this way seems to put the component // in a state where it believes it has focus. In reality // the table retains focus - though it is difficult for // a user to tell, since the caret is visible and flashing. // Calling table.requestFocus() here, to get the focus back to // the table, seems to have no effect. Component editorComp = table.getEditorComponent(); if (table.isEditing() && editorComp != null) { if (editorComp instanceof JComponent) { JComponent component = (JComponent)editorComp; map = component.getInputMap(JComponent.WHEN_FOCUSED); Object binding = (map != null) ? map.get(keyStroke) : null; if (binding == null) { map = component.getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); binding = (map != null) ? map.get(keyStroke) : null; } if (binding != null) { ActionMap am = component.getActionMap(); Action action = (am != null) ? am.get(binding) : null; if (action != null && SwingUtilities. notifyAction(action, keyStroke, e, component, e.getModifiers())) { e.consume(); } } } } } // MouseInputListener // Component receiving mouse events during editing. // May not be editorComponent. protected Component dispatchComponent; private boolean selectedOnPress; public void mouseClicked(MouseEvent e) {} protected 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); SwingUtilities2.setSkipClickCount(dispatchComponent, e.getClickCount() - 1); } protected 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; } protected void setValueIsAdjusting(boolean flag) { table.getSelectionModel().setValueIsAdjusting(flag); table.getColumnModel().getSelectionModel(). setValueIsAdjusting(flag); } private boolean shouldIgnore0(MouseEvent e) { return e.isConsumed() || SwingUtilities2.shouldIgnore(e, table); } public void mousePressed(MouseEvent e) { if (e.isConsumed()) { selectedOnPress = false; return; } selectedOnPress = true; adjustFocusAndSelection(e); } void adjustFocusAndSelection(MouseEvent e) { if (shouldIgnore0(e)) { return; } Point p = e.getPoint(); int row = table.rowAtPoint(p); int column = table.columnAtPoint(p); // Fix for 4835633 if (pointOutsidePrefSize(table, row, column, p)) { // If shift is down in multi-select, we should just return. // For single select or non-shift-click, clear the selection if (e.getID() == MouseEvent.MOUSE_PRESSED && (!e.isShiftDown() || table.getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)) { table.clearSelection(); TableCellEditor tce = table.getCellEditor(); if (tce != null) { tce.stopCellEditing(); } } return; } // 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 { SwingUtilities2.adjustFocus(table); } CellEditor editor = table.getCellEditor(); if (editor == null || editor.shouldSelectCell(e)) { boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ? true : false; setValueIsAdjusting(adjusting); makeSelectionChange(row, column, e); } } protected void makeSelectionChange(int row, int column, MouseEvent e) { boolean ctrl = e.isControlDown(); // Apply the selection state of the anchor to all cells between it and the // current cell, and then select the current cell. // For mustang, where API changes are allowed, this logic will moved to // JTable.changeSelection() if (ctrl && e.isShiftDown()) { ListSelectionModel rm = table.getSelectionModel(); ListSelectionModel cm = table.getColumnModel().getSelectionModel(); int anchorRow = rm.getAnchorSelectionIndex(); int anchorCol = cm.getAnchorSelectionIndex(); boolean anchorSelected = true; if (anchorRow == -1 || anchorRow >= table.getRowCount()) { anchorRow = 0; anchorSelected = false; } if (anchorCol == -1 || anchorCol >= table.getColumnCount()) { anchorCol = 0; anchorSelected = false; } if (anchorSelected && table.isCellSelected(anchorRow, anchorCol)) { rm.addSelectionInterval(anchorRow, row); cm.addSelectionInterval(anchorCol, column); } else { rm.removeSelectionInterval(anchorRow, row); cm.removeSelectionInterval(anchorCol, column); // This is only to match the windows explorer behavior in JFileChooser if (isFileList) { rm.addSelectionInterval(row, row); rm.setAnchorSelectionIndex(anchorRow); cm.addSelectionInterval(column, column); cm.setAnchorSelectionIndex(anchorCol); } } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -