📄 basiccomboboxui.java
字号:
* explicitly set with <code>setRenderer</code>. * * @return a <code>ListCellRender</code> used for the combo box * @see javax.swing.JComboBox#setRenderer */ protected ListCellRenderer createRenderer() { return new BasicComboBoxRenderer.UIResource(); } /** * Creates the default editor that will be used in editable combo boxes. * A default editor will be used only if an editor has not been * explicitly set with <code>setEditor</code>. * * @return a <code>ComboBoxEditor</code> used for the combo box * @see javax.swing.JComboBox#setEditor */ protected ComboBoxEditor createEditor() { return new BasicComboBoxEditor.UIResource(); } // // end UI Initialization //====================== //====================== // begin Inner classes // /** * This listener checks to see if the key event isn't a navigation key. If * it finds a key event that wasn't a navigation key it dispatches it to * JComboBox.selectWithKeyChar() so that it can do type-ahead. * * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. */ public class KeyHandler extends KeyAdapter { public void keyPressed( KeyEvent e ) { if ( comboBox.isEnabled() && !isNavigationKey( e.getKeyCode() ) && isTypeAheadKey( e ) ) { if ( comboBox.selectWithKeyChar(e.getKeyChar()) ) { e.consume(); } } } boolean isTypeAheadKey( KeyEvent e ) { return !e.isAltDown() && !e.isControlDown() && !e.isMetaDown(); } } /** * This listener hides the popup when the focus is lost. It also repaints * when focus is gained or lost. * * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. */ public class FocusHandler implements FocusListener { public void focusGained( FocusEvent e ) { hasFocus = true; comboBox.repaint(); if (comboBox.isEditable() && editor != null) { editor.requestFocus(); } // Notify assistive technologies that the combo box // gained focus. if (comboBox instanceof Accessible) { AccessibleContext ac = ((Accessible)comboBox).getAccessibleContext(); if (ac != null) { ac.firePropertyChange( AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED); } } } public void focusLost( FocusEvent e ) { hasFocus = false; // GES, 980818: // Note that the second check here is a workaround to bug // 4168483. There is a bogus focusLost sent to the // ComboBox with isTemporary false when a mediumweight menu // is popped up. Until this is fixed in AWT, we make the // tradeoff of not popping down mediumweight popups when // the combobox loses focus. Although this means that the // combobox does not remove such menus when you tab out, // it is seen as more desirable than the alternative which // is that mediumweight combobox menus dissappear immediately // on popup, rendering them completely unusable. if ( !e.isTemporary() && comboBox.isLightWeightPopupEnabled()) { setPopupVisible(comboBox, false); } comboBox.repaint(); // Notify assistive technologies that the combo box // lost focus. if (comboBox instanceof Accessible) { AccessibleContext ac = ((Accessible)comboBox).getAccessibleContext(); if (ac != null) { ac.firePropertyChange( AccessibleContext.ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null); } } } } /** * This listener watches for changes in the * <code>ComboBoxModel</code>. * <p> * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. * * @see #createListDataListener */ public class ListDataHandler implements ListDataListener { public void contentsChanged( ListDataEvent e ) { if ( !(e.getIndex0() == -1 && e.getIndex1() == -1) ) { isMinimumSizeDirty = true; comboBox.revalidate(); } // set the editor with the selected item since this // is the event handler for a selected item change. if (comboBox.isEditable() && editor != null) { comboBox.configureEditor( comboBox.getEditor(), comboBox.getSelectedItem() ); } comboBox.repaint(); } public void intervalAdded( ListDataEvent e ) { isDisplaySizeDirty = true; contentsChanged( e ); } public void intervalRemoved( ListDataEvent e ) { isDisplaySizeDirty = true; contentsChanged( e ); } } /** * This listener watches for changes to the selection in the * combo box. * <p> * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. * * @see #createItemListener */ public class ItemHandler implements ItemListener { // This class used to implement behavior which is now redundant. public void itemStateChanged(ItemEvent e) {} } /** * This listener watches for bound properties that have changed in the * combo box. * <p> * Subclasses which wish to listen to combo box property changes should * call the superclass methods to ensure that the combo box ui correctly * handles property changes. * <p> * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. * * @see #createPropertyChangeListener */ public class PropertyChangeHandler implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); JComboBox comboBox = (JComboBox)e.getSource(); if ( propertyName.equals( "model" ) ) { ComboBoxModel newModel = (ComboBoxModel)e.getNewValue(); ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue(); if ( oldModel != null && listDataListener != null ) { oldModel.removeListDataListener( listDataListener ); } if ( newModel != null && listDataListener != null ) { newModel.addListDataListener( listDataListener ); } if ( editor != null ) { comboBox.configureEditor( comboBox.getEditor(), comboBox.getSelectedItem() ); } isMinimumSizeDirty = true; isDisplaySizeDirty = true; comboBox.revalidate(); comboBox.repaint(); } else if ( propertyName.equals( "editor" ) && comboBox.isEditable() ) { addEditor(); comboBox.revalidate(); } else if ( propertyName.equals( "editable" ) ) { if ( comboBox.isEditable() ) { comboBox.setRequestFocusEnabled( false ); addEditor(); } else { comboBox.setRequestFocusEnabled( true ); removeEditor(); } updateToolTipTextForChildren(); comboBox.revalidate(); } else if ( propertyName.equals( "enabled" ) ) { boolean enabled = comboBox.isEnabled(); if ( editor != null ) editor.setEnabled(enabled); if ( arrowButton != null ) arrowButton.setEnabled(enabled); comboBox.repaint(); } else if ( propertyName.equals( "maximumRowCount" ) ) { if ( isPopupVisible( comboBox ) ) { setPopupVisible(comboBox, false); setPopupVisible(comboBox, true); } } else if ( propertyName.equals( "font" ) ) { listBox.setFont( comboBox.getFont() ); if ( editor != null ) { editor.setFont( comboBox.getFont() ); } isMinimumSizeDirty = true; comboBox.validate(); } else if ( propertyName.equals( JComponent.TOOL_TIP_TEXT_KEY ) ) { updateToolTipTextForChildren(); } else if ( propertyName.equals( BasicComboBoxUI.IS_TABLE_CELL_EDITOR ) ) { Boolean inTable = (Boolean)e.getNewValue(); isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false; } else if (propertyName.equals("prototypeDisplayValue")) { isMinimumSizeDirty = true; isDisplaySizeDirty = true; comboBox.revalidate(); } else if (propertyName.equals("renderer")) { isMinimumSizeDirty = true; isDisplaySizeDirty = true; comboBox.revalidate(); } } } // Syncronizes the ToolTip text for the components within the combo box to be the // same value as the combo box ToolTip text. private void updateToolTipTextForChildren() { Component[] children = comboBox.getComponents(); for ( int i = 0; i < children.length; ++i ) { if ( children[i] instanceof JComponent ) { ((JComponent)children[i]).setToolTipText( comboBox.getToolTipText() ); } } } /** * This layout manager handles the 'standard' layout of combo boxes. It puts * the arrow button to the right and the editor to the left. If there is no * editor it still keeps the arrow button to the right. * * This public inner class should be treated as protected. * Instantiate it only within subclasses of * <code>BasicComboBoxUI</code>. */ public class ComboBoxLayoutManager implements LayoutManager { public void addLayoutComponent(String name, Component comp) {} public void removeLayoutComponent(Component comp) {} public Dimension preferredLayoutSize(Container parent) { JComboBox cb = (JComboBox)parent; return parent.getPreferredSize(); } public Dimension minimumLayoutSize(Container parent) { JComboBox cb = (JComboBox)parent; return parent.getMinimumSize(); } public void layoutContainer(Container parent) { JComboBox cb = (JComboBox)parent; int width = cb.getWidth(); int height = cb.getHeight(); Insets insets = getInsets(); int buttonSize = height - (insets.top + insets.bottom); Rectangle cvb; if ( arrowButton != null ) { if(BasicGraphicsUtils.isLeftToRight(cb)) { arrowButton.setBounds( width - (insets.right + buttonSize), insets.top, buttonSize, buttonSize); } else { arrowButton.setBounds( insets.left, insets.top, buttonSize, buttonSize); } } if ( editor != null ) { cvb = rectangleForCurrentValue(); editor.setBounds(cvb); } } } // // end Inner classes //==================== //=============================== // begin Sub-Component Management // /** * Creates and initializes the components which make up the * aggregate combo box. This method is called as part of the UI * installation process. */ protected void installComponents() { arrowButton = createArrowButton(); comboBox.add( arrowButton ); if (arrowButton != null) { configureArrowButton(); } if ( comboBox.isEditable() ) { addEditor(); } comboBox.add( currentValuePane ); } /** * The aggregate components which compise the combo box are * unregistered and uninitialized. This method is called as part of the * UI uninstallation process. */ protected void uninstallComponents() { if ( arrowButton != null ) { unconfigureArrowButton(); } if ( editor != null ) { unconfigureEditor(); } comboBox.removeAll(); // Just to be safe. arrowButton = null; } /** * This public method is implementation specific and should be private. * do not call or override. To implement a specific editor create a * custom <code>ComboBoxEditor</code> * * @see #createEditor * @see javax.swing.JComboBox#setEditor * @see javax.swing.ComboBoxEditor */ public void addEditor() { removeEditor(); editor = comboBox.getEditor().getEditorComponent(); if ( editor != null ) { configureEditor(); comboBox.add(editor); } } /** * This public method is implementation specific and should be private. * do not call or override. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -