📄 basiccomboboxui.java
字号:
protected void unconfigureEditor() { if (focusListener != null) { editor.removeFocusListener(focusListener); } editor.removeFocusListener(getHandler()); comboBox.getEditor().removeActionListener(getHandler()); } /** * This public method is implementation specific and should be private. Do * not call or override. * * @see #createArrowButton */ public void configureArrowButton() { if ( arrowButton != null ) { arrowButton.setEnabled( comboBox.isEnabled() ); arrowButton.setFocusable(comboBox.isFocusable()); arrowButton.setRequestFocusEnabled(false); arrowButton.addMouseListener( popup.getMouseListener() ); arrowButton.addMouseMotionListener( popup.getMouseMotionListener() ); arrowButton.resetKeyboardActions(); arrowButton.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY); arrowButton.setInheritsPopupMenu(true); } } /** * This public method is implementation specific and should be private. Do * not call or override. * * @see #createArrowButton */ public void unconfigureArrowButton() { if ( arrowButton != null ) { arrowButton.removeMouseListener( popup.getMouseListener() ); arrowButton.removeMouseMotionListener( popup.getMouseMotionListener() ); } } /** * Creates an button which will be used as the control to show or hide * the popup portion of the combo box. * * @return a button which represents the popup control */ protected JButton createArrowButton() { JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, UIManager.getColor("ComboBox.buttonBackground"), UIManager.getColor("ComboBox.buttonShadow"), UIManager.getColor("ComboBox.buttonDarkShadow"), UIManager.getColor("ComboBox.buttonHighlight")); button.setName("ComboBox.arrowButton"); return button; } // // end Sub-Component Management //=============================== //================================ // begin ComboBoxUI Implementation // /** * Tells if the popup is visible or not. */ public boolean isPopupVisible( JComboBox c ) { return popup.isVisible(); } /** * Hides the popup. */ public void setPopupVisible( JComboBox c, boolean v ) { if ( v ) { popup.show(); } else { popup.hide(); } } /** * Determines if the JComboBox is focus traversable. If the JComboBox is editable * this returns false, otherwise it returns true. */ public boolean isFocusTraversable( JComboBox c ) { return !comboBox.isEditable(); } // // end ComboBoxUI Implementation //============================== //================================= // begin ComponentUI Implementation public void paint( Graphics g, JComponent c ) { hasFocus = comboBox.hasFocus(); if ( !comboBox.isEditable() ) { Rectangle r = rectangleForCurrentValue(); paintCurrentValueBackground(g,r,hasFocus); paintCurrentValue(g,r,hasFocus); } } public Dimension getPreferredSize( JComponent c ) { return getMinimumSize(c); } /** * The minumum size is the size of the display area plus insets plus the button. */ public Dimension getMinimumSize( JComponent c ) { if ( !isMinimumSizeDirty ) { return new Dimension(cachedMinimumSize); } Dimension size = getDisplaySize(); Insets insets = getInsets(); size.height += insets.top + insets.bottom; int buttonSize = size.height - (insets.top + insets.bottom); size.width += insets.left + insets.right + buttonSize; cachedMinimumSize.setSize( size.width, size.height ); isMinimumSizeDirty = false; return new Dimension(size); } public Dimension getMaximumSize( JComponent c ) { return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); } /** * 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); int baseline = -1; // force sameBaseline to be updated. getDisplaySize(); if (sameBaseline) { Insets insets = c.getInsets(); height = height - insets.top - insets.bottom; if (!comboBox.isEditable()) { ListCellRenderer renderer = comboBox.getRenderer(); if (renderer == null) { renderer = new DefaultListCellRenderer(); } Object value = null; Object prototypeValue = comboBox.getPrototypeDisplayValue(); if (prototypeValue != null) { value = prototypeValue; } else if (comboBox.getModel().getSize() > 0) { // Note, we're assuming the baseline is the same for all // cells, if not, this needs to loop through all. value = comboBox.getModel().getElementAt(0); } if (value == null) { value = " "; } else if (value instanceof String && "".equals(value)) { value = " "; } Component component = renderer. getListCellRendererComponent(listBox, value, -1, false, false); if (component instanceof JComponent) { component.setFont(comboBox.getFont()); } baseline = component.getBaseline(width, height); } else { baseline = editor.getBaseline(width, height); } if (baseline > 0) { baseline += insets.top; } } return baseline; } /** * 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); // Force sameBaseline to be updated. getDisplaySize(); if (comboBox.isEditable()) { return editor.getBaselineResizeBehavior(); } else if (sameBaseline) { ListCellRenderer renderer = comboBox.getRenderer(); if (renderer == null) { renderer = new DefaultListCellRenderer(); } Object value = null; Object prototypeValue = comboBox.getPrototypeDisplayValue(); if (prototypeValue != null) { value = prototypeValue; } else if (comboBox.getModel().getSize() > 0) { // Note, we're assuming the baseline is the same for all // cells, if not, this needs to loop through all. value = comboBox.getModel().getElementAt(0); } if (value != null) { Component component = renderer. getListCellRendererComponent(listBox, value, -1, false, false); return component.getBaselineResizeBehavior(); } } return Component.BaselineResizeBehavior.OTHER; } // This is currently hacky... public int getAccessibleChildrenCount(JComponent c) { if ( comboBox.isEditable() ) { return 2; } else { return 1; } } // This is currently hacky... public Accessible getAccessibleChild(JComponent c, int i) { // 0 = the popup // 1 = the editor switch ( i ) { case 0: if ( popup instanceof Accessible ) { AccessibleContext ac = ((Accessible) popup).getAccessibleContext(); ac.setAccessibleParent(comboBox); return(Accessible) popup; } break; case 1: if ( comboBox.isEditable() && (editor instanceof Accessible) ) { AccessibleContext ac = ((Accessible) editor).getAccessibleContext(); ac.setAccessibleParent(comboBox); return(Accessible) editor; } break; } return null; } // // end ComponentUI Implementation //=============================== //====================== // begin Utility Methods // /** * Returns whether or not the supplied keyCode maps to a key that is used for * navigation. This is used for optimizing key input by only passing non- * navigation keys to the type-ahead mechanism. Subclasses should override this * if they change the navigation keys. */ protected boolean isNavigationKey( int keyCode ) { return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_KP_DOWN; } private boolean isNavigationKey(int keyCode, int modifiers) { InputMap inputMap = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke key = KeyStroke.getKeyStroke(keyCode, modifiers); if (inputMap != null && inputMap.get(key) != null) { return true; } return false; } /** * Selects the next item in the list. It won't change the selection if the * currently selected item is already the last item. */ protected void selectNextPossibleValue() { int si; if ( isTableCellEditor ) { si = listBox.getSelectedIndex(); } else { si = comboBox.getSelectedIndex(); } if ( si < comboBox.getModel().getSize() - 1 ) { if ( isTableCellEditor ) { listBox.setSelectedIndex( si + 1 ); listBox.ensureIndexIsVisible( si + 1 ); } else { comboBox.setSelectedIndex(si+1); } comboBox.repaint(); } } /** * Selects the previous item in the list. It won't change the selection if the * currently selected item is already the first item. */ protected void selectPreviousPossibleValue() { int si; if ( isTableCellEditor ) { si = listBox.getSelectedIndex(); } else { si = comboBox.getSelectedIndex(); } if ( si > 0 ) { if ( isTableCellEditor ) { listBox.setSelectedIndex( si - 1 ); listBox.ensureIndexIsVisible( si - 1 ); } else { comboBox.setSelectedIndex(si-1); } comboBox.repaint(); } } /** * Hides the popup if it is showing and shows the popup if it is hidden. */ protected void toggleOpenClose() { setPopupVisible(comboBox, !isPopupVisible(comboBox)); } /** * Returns the area that is reserved for drawing the currently selected item. */ protected Rectangle rectangleForCurrentValue() { int width = comboBox.getWidth(); int height = comboBox.getHeight(); Insets insets = getInsets(); int buttonSize = height - (insets.top + insets.bottom); if ( arrowButton != null ) { buttonSize = arrowButton.getWidth(); } if(BasicGraphicsUtils.isLeftToRight(comboBox)) { return new Rectangle(insets.left, insets.top, width - (insets.left + insets.right + buttonSize), height - (insets.top + insets.bottom)); } else { return new Rectangle(insets.left + buttonSize, insets.top, width - (insets.left + insets.right + buttonSize), height - (insets.top + insets.bottom)); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -