📄 basicspinnerui.java
字号:
if (start != -1 && end != -1 && start <= max && end <= max) { ftf.select(start, end); } return true; } } while (iterator.next() != CharacterIterator.DONE); return false; } /** * Returns the calendarField under the start of the selection, or * -1 if there is no valid calendar field under the selection (or * the spinner isn't editing dates. */ private int getCalendarField(JSpinner spinner) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DateEditor) { JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor; JFormattedTextField ftf = dateEditor.getTextField(); int start = ftf.getSelectionStart(); JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); if (formatter instanceof InternationalFormatter) { Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start); for (int counter = 0; counter < fields.length; counter++) { if (fields[counter] instanceof DateFormat.Field) { int calendarField; if (fields[counter] == DateFormat.Field.HOUR1) { calendarField = Calendar.HOUR; } else { calendarField = ((DateFormat.Field) fields[counter]).getCalendarField(); } if (calendarField != -1) { return calendarField; } } } } } return -1; } public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) { spinner = eventToSpinner(e); autoRepeatTimer.start(); focusSpinnerIfNecessary(); } } public void mouseReleased(MouseEvent e) { autoRepeatTimer.stop(); arrowButton = null; spinner = null; } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { if (spinner != null && !autoRepeatTimer.isRunning() && spinner == eventToSpinner(e)) { autoRepeatTimer.start(); } } public void mouseExited(MouseEvent e) { if (autoRepeatTimer.isRunning()) { autoRepeatTimer.stop(); } } /** * Requests focus on a child of the spinner if the spinner doesn't * have focus. */ private void focusSpinnerIfNecessary() { Component fo = KeyboardFocusManager. getCurrentKeyboardFocusManager().getFocusOwner(); if (spinner.isRequestFocusEnabled() && ( fo == null || !SwingUtilities.isDescendingFrom(fo, spinner))) { Container root = spinner; if (!root.isFocusCycleRoot()) { root = root.getFocusCycleRootAncestor(); } if (root != null) { FocusTraversalPolicy ftp = root.getFocusTraversalPolicy(); Component child = ftp.getComponentAfter(root, spinner); if (child != null && SwingUtilities.isDescendingFrom( child, spinner)) { child.requestFocus(); } } } } public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { if (spinner == eventToSpinner(e)) { if (autoRepeatTimer.isRunning()) { autoRepeatTimer.stop(); } spinner = null; if (arrowButton != null) { ButtonModel model = arrowButton.getModel(); model.setPressed(false); model.setArmed(false); arrowButton = null; } } } } private static class Handler implements LayoutManager, PropertyChangeListener, ChangeListener { // // LayoutManager // private Component nextButton = null; private Component previousButton = null; private Component editor = null; public void addLayoutComponent(String name, Component c) { if ("Next".equals(name)) { nextButton = c; } else if ("Previous".equals(name)) { previousButton = c; } else if ("Editor".equals(name)) { editor = c; } } public void removeLayoutComponent(Component c) { if (c == nextButton) { c = null; } else if (c == previousButton) { previousButton = null; } else if (c == editor) { editor = null; } } private Dimension preferredSize(Component c) { return (c == null) ? zeroSize : c.getPreferredSize(); } public Dimension preferredLayoutSize(Container parent) { Dimension nextD = preferredSize(nextButton); Dimension previousD = preferredSize(previousButton); Dimension editorD = preferredSize(editor); /* Force the editors height to be a multiple of 2 */ editorD.height = ((editorD.height + 1) / 2) * 2; Dimension size = new Dimension(editorD.width, editorD.height); size.width += Math.max(nextD.width, previousD.width); Insets insets = parent.getInsets(); size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; return size; } public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } private void setBounds(Component c, int x, int y, int width, int height) { if (c != null) { c.setBounds(x, y, width, height); } } public void layoutContainer(Container parent) { int width = parent.getWidth(); int height = parent.getHeight(); Insets insets = parent.getInsets(); Dimension nextD = preferredSize(nextButton); Dimension previousD = preferredSize(previousButton); int buttonsWidth = Math.max(nextD.width, previousD.width); int editorHeight = height - (insets.top + insets.bottom); // The arrowButtonInsets value is used instead of the JSpinner's // insets if not null. Defining this to be (0, 0, 0, 0) causes the // buttons to be aligned with the outer edge of the spinner's // border, and leaving it as "null" places the buttons completely // inside the spinner's border. Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets"); if (buttonInsets == null) { buttonInsets = insets; } /* Deal with the spinner's componentOrientation property. */ int editorX, editorWidth, buttonsX; if (parent.getComponentOrientation().isLeftToRight()) { editorX = insets.left; editorWidth = width - insets.left - buttonsWidth - buttonInsets.right; buttonsX = width - buttonsWidth - buttonInsets.right; } else { buttonsX = buttonInsets.left; editorX = buttonsX + buttonsWidth; editorWidth = width - buttonInsets.left - buttonsWidth - insets.right; } int nextY = buttonInsets.top; int nextHeight = (height / 2) + (height % 2) - nextY; int previousY = buttonInsets.top + nextHeight; int previousHeight = height - previousY - buttonInsets.bottom; setBounds(editor, editorX, insets.top, editorWidth, editorHeight); setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight); setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight); } // // PropertyChangeListener // public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if (e.getSource() instanceof JSpinner) { JSpinner spinner = (JSpinner)(e.getSource()); SpinnerUI spinnerUI = spinner.getUI(); if (spinnerUI instanceof BasicSpinnerUI) { BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI; if ("editor".equals(propertyName)) { JComponent oldEditor = (JComponent)e.getOldValue(); JComponent newEditor = (JComponent)e.getNewValue(); ui.replaceEditor(oldEditor, newEditor); ui.updateEnabledState(); if (oldEditor instanceof JSpinner.DefaultEditor) { JTextField tf = ((JSpinner.DefaultEditor)oldEditor).getTextField(); if (tf != null) { tf.removeFocusListener(nextButtonHandler); tf.removeFocusListener(previousButtonHandler); } } if (newEditor instanceof JSpinner.DefaultEditor) { JTextField tf = ((JSpinner.DefaultEditor)newEditor).getTextField(); if (tf != null) { if (tf.getFont() instanceof UIResource) { tf.setFont(spinner.getFont()); } tf.addFocusListener(nextButtonHandler); tf.addFocusListener(previousButtonHandler); } } if (newEditor instanceof JSpinner.DefaultEditor) { JTextField tf = ((JSpinner.DefaultEditor)newEditor).getTextField(); if (tf != null) { if (tf.getFont() instanceof UIResource) { tf.setFont(spinner.getFont()); } } } } else if ("enabled".equals(propertyName) || "model".equals(propertyName)) { ui.updateEnabledState(); } else if ("font".equals(propertyName)) { JComponent editor = spinner.getEditor(); if (editor!=null && editor instanceof JSpinner.DefaultEditor) { JTextField tf = ((JSpinner.DefaultEditor)editor).getTextField(); if (tf != null) { if (tf.getFont() instanceof UIResource) { tf.setFont(spinner.getFont()); } } } } else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) { updateToolTipTextForChildren(spinner); } } } else if (e.getSource() instanceof JComponent) { JComponent c = (JComponent)e.getSource(); if ((c.getParent() instanceof JPanel) && (c.getParent().getParent() instanceof JSpinner) && "border".equals(propertyName)) { JSpinner spinner = (JSpinner)c.getParent().getParent(); SpinnerUI spinnerUI = spinner.getUI(); if (spinnerUI instanceof BasicSpinnerUI) { BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI; ui.maybeRemoveEditorBorder(c); } } } } // Syncronizes the ToolTip text for the components within the spinner // to be the same value as the spinner ToolTip text. private void updateToolTipTextForChildren(JComponent spinner) { String toolTipText = spinner.getToolTipText(); Component[] children = spinner.getComponents(); for (int i = 0; i < children.length; i++) { if (children[i] instanceof JSpinner.DefaultEditor) { JTextField tf = ((JSpinner.DefaultEditor)children[i]).getTextField(); if (tf != null) { tf.setToolTipText(toolTipText); } } else if (children[i] instanceof JComponent) { ((JComponent)children[i]).setToolTipText( spinner.getToolTipText() ); } } } public void stateChanged(ChangeEvent e) { if (e.getSource() instanceof JSpinner) { JSpinner spinner = (JSpinner)e.getSource(); SpinnerUI spinnerUI = spinner.getUI(); if (DefaultLookup.getBoolean(spinner, spinnerUI, "Spinner.disableOnBoundaryValues", false) && spinnerUI instanceof BasicSpinnerUI) { BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI; ui.updateEnabledState(); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -