?? basiccomboboxui.java
字號:
* be added to the combo box. * * @return an instance of a <code>PropertyChangeListener</code> or null */ protected PropertyChangeListener createPropertyChangeListener() { return getHandler(); } /** * Creates a layout manager for managing the components which make up the * combo box. * * @return an instance of a layout manager */ protected LayoutManager createLayoutManager() { return getHandler(); } /** * Creates the default renderer that will be used in a non-editiable combo * box. A default renderer will used only if a renderer has not been * 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(); } /** * Returns the shared listener. */ private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } // // 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 ) { getHandler().keyPressed(e); } } /** * 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 ) { getHandler().focusGained(e); } public void focusLost( FocusEvent e ) { getHandler().focusLost(e); } } /** * 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 ) { getHandler().contentsChanged(e); } public void intervalAdded( ListDataEvent e ) { getHandler().intervalAdded(e); } public void intervalRemoved( ListDataEvent e ) { getHandler().intervalRemoved(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) { getHandler().propertyChange(e); } } // 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) { return getHandler().preferredLayoutSize(parent); } public Dimension minimumLayoutSize(Container parent) { return getHandler().minimumLayoutSize(parent); } public void layoutContainer(Container parent) { getHandler().layoutContainer(parent); } } // // 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); if(comboBox.isFocusOwner()) { // Switch focus to the editor component editor.requestFocusInWindow(); } } } /** * This public method is implementation specific and should be private. * do not call or override. * * @see #addEditor */ public void removeEditor() { if ( editor != null ) { unconfigureEditor(); comboBox.remove( editor ); editor = null; } } /** * This protected method is implementation specific and should be private. * do not call or override. * * @see #addEditor */ protected void configureEditor() { // Should be in the same state as the combobox editor.setEnabled(comboBox.isEnabled()); editor.setFont( comboBox.getFont() ); if (focusListener != null) { editor.addFocusListener(focusListener); } editor.addFocusListener( getHandler() ); comboBox.getEditor().addActionListener(getHandler()); if(editor instanceof JComponent) { ((JComponent)editor).putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY); ((JComponent)editor).setInheritsPopupMenu(true); } comboBox.configureEditor(comboBox.getEditor(),comboBox.getSelectedItem()); } /** * This protected method is implementation specific and should be private. * Do not call or override. * * @see #addEditor */ 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.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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -