📄 basictableui.java
字号:
setValueIsAdjusting(true); } adjustSelection(e); } } private void mousePressedDND(MouseEvent e) { pressedEvent = e; boolean grabFocus = true; dragStarted = false; if (canStartDrag() && DragRecognitionSupport.mousePressed(e)) { dragPressDidSelection = false; if (e.isControlDown() && isFileList) { // do nothing for control - will be handled on release // or when drag starts return; } else if (!e.isShiftDown() && table.isCellSelected(pressedRow, pressedCol)) { // clicking on something that's already selected // and need to make it the lead now table.getSelectionModel().addSelectionInterval(pressedRow, pressedRow); table.getColumnModel().getSelectionModel(). addSelectionInterval(pressedCol, pressedCol); return; } dragPressDidSelection = true; // could be a drag initiating event - don't grab focus grabFocus = false; } else if (!isFileList) { // When drag can't happen, mouse drags might change the selection in the table // so we want the isAdjusting flag to be set setValueIsAdjusting(true); } if (grabFocus) { SwingUtilities2.adjustFocus(table); } adjustSelection(e); } private void adjustSelection(MouseEvent e) { // Fix for 4835633 if (outsidePrefSize) { // 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 ((pressedCol == -1) || (pressedRow == -1)) { return; } boolean dragEnabled = table.getDragEnabled(); if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) { setDispatchComponent(e); repostEvent(e); } CellEditor editor = table.getCellEditor(); if (dragEnabled || editor == null || editor.shouldSelectCell(e)) { table.changeSelection(pressedRow, pressedCol, e.isControlDown(), e.isShiftDown()); } } public void valueChanged(ListSelectionEvent e) { if (timer != null) { timer.stop(); timer = null; } } public void actionPerformed(ActionEvent ae) { table.editCellAt(pressedRow, pressedCol, null); Component editorComponent = table.getEditorComponent(); if (editorComponent != null && !editorComponent.hasFocus()) { SwingUtilities2.compositeRequestFocus(editorComponent); } return; } private void maybeStartTimer() { if (!shouldStartTimer) { return; } if (timer == null) { timer = new Timer(1200, this); timer.setRepeats(false); } timer.start(); } public void mouseReleased(MouseEvent e) { if (SwingUtilities2.shouldIgnore(e, table)) { return; } if (table.getDragEnabled()) { mouseReleasedDND(e); } else { if (isFileList) { maybeStartTimer(); } } pressedEvent = null; repostEvent(e); dispatchComponent = null; setValueIsAdjusting(false); } private void mouseReleasedDND(MouseEvent e) { MouseEvent me = DragRecognitionSupport.mouseReleased(e); if (me != null) { SwingUtilities2.adjustFocus(table); if (!dragPressDidSelection) { adjustSelection(me); } } if (!dragStarted) { if (isFileList) { maybeStartTimer(); return; } Point p = e.getPoint(); if (pressedEvent != null && table.rowAtPoint(p) == pressedRow && table.columnAtPoint(p) == pressedCol && table.editCellAt(pressedRow, pressedCol, pressedEvent)) { setDispatchComponent(pressedEvent); repostEvent(pressedEvent); // This may appear completely odd, but must be done for backward // compatibility reasons. Developers have been known to rely on // a call to shouldSelectCell after editing has begun. CellEditor ce = table.getCellEditor(); if (ce != null) { ce.shouldSelectCell(pressedEvent); } } } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void dragStarting(MouseEvent me) { dragStarted = true; if (me.isControlDown() && isFileList) { table.getSelectionModel().addSelectionInterval(pressedRow, pressedRow); table.getColumnModel().getSelectionModel(). addSelectionInterval(pressedCol, pressedCol); } pressedEvent = null; } public void mouseDragged(MouseEvent e) { if (SwingUtilities2.shouldIgnore(e, table)) { return; } if (table.getDragEnabled() && (DragRecognitionSupport.mouseDragged(e, this) || dragStarted)) { return; } repostEvent(e); // Check isFileList: // Until we support drag-selection, dragging should not change // the selection (act like single-select). if (isFileList || table.isEditing()) { 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; } table.changeSelection(row, column, e.isControlDown(), true); } // PropertyChangeListener public void propertyChange(PropertyChangeEvent event) { String changeName = event.getPropertyName(); if ("componentOrientation" == changeName) { JTableHeader header = table.getTableHeader(); if (header != null) { header.setComponentOrientation( (ComponentOrientation)event.getNewValue()); } } else if ("dropLocation" == changeName) { JTable.DropLocation oldValue = (JTable.DropLocation)event.getOldValue(); repaintDropLocation(oldValue); repaintDropLocation(table.getDropLocation()); } else if ("Table.isFileList" == changeName) { isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList")); table.revalidate(); table.repaint(); if (isFileList) { table.getSelectionModel().addListSelectionListener(getHandler()); } else { table.getSelectionModel().removeListSelectionListener(getHandler()); timer = null; } } else if ("selectionModel" == changeName) { if (isFileList) { ListSelectionModel old = (ListSelectionModel)event.getOldValue(); old.removeListSelectionListener(getHandler()); table.getSelectionModel().addListSelectionListener(getHandler()); } } } private void repaintDropLocation(JTable.DropLocation loc) { if (loc == null) { return; } if (!loc.isInsertRow() && !loc.isInsertColumn()) { Rectangle rect = table.getCellRect(loc.getRow(), loc.getColumn(), false); if (rect != null) { table.repaint(rect); } return; } if (loc.isInsertRow()) { Rectangle rect = extendRect(getHDropLineRect(loc), true); if (rect != null) { table.repaint(rect); } } if (loc.isInsertColumn()) { Rectangle rect = extendRect(getVDropLineRect(loc), false); if (rect != null) { table.repaint(rect); } } } } /* * Returns true if the given point is outside the preferredSize of the * item at the given row of the table. (Column must be 0). * Returns false if the "Table.isFileList" client property is not set. */ private boolean pointOutsidePrefSize(int row, int column, Point p) { if (!isFileList) { return false; } return SwingUtilities2.pointOutsidePrefSize(table, row, column, p); }//// Factory methods for the Listeners// private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } /** * 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 getHandler(); } /** * Creates the mouse listener for the JTable. */ protected MouseInputListener createMouseInputListener() { return getHandler(); }//// The installation/uninstall procedures and support// public static ComponentUI createUI(JComponent c) { return new BasicTableUI(); }// Installation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -