📄 basiccomboboxui.java
字号:
} /** * Creates and returns a layout manager for the combo box. Subclasses can * override this method to provide a different layout. * * @return a layout manager for the combo box. */ protected LayoutManager createLayoutManager() { return new ComboBoxLayoutManager(); } /** * Creates a component that will be responsible for rendering the * selected component in the combo box. * * @return A renderer for the combo box. */ protected ListCellRenderer createRenderer() { return new BasicComboBoxRenderer(); } /** * Creates the component that will be responsible for displaying/editing * the selected item in the combo box. This editor is used only when combo * box is editable. * * @return A new component that will be responsible for displaying/editing * the selected item in the combo box. */ protected ComboBoxEditor createEditor() { return new BasicComboBoxEditor.UIResource(); } /** * Installs the components for this JComboBox. ArrowButton, main * part of combo box (upper part) and popup list of items are created and * configured here. */ protected void installComponents() { // create drop down list of items popup = createPopup(); listBox = popup.getList(); // set editor and renderer for the combo box. Editor is used // only if combo box becomes editable, otherwise renderer is used // to paint the selected item; combobox is not editable by default. comboBox.setRenderer(createRenderer()); // create and install arrow button arrowButton = createArrowButton(); configureArrowButton(); comboBox.add(arrowButton); ComboBoxEditor currentEditor = comboBox.getEditor(); if (currentEditor == null || currentEditor instanceof UIResource) { currentEditor = createEditor(); comboBox.setEditor(currentEditor); } editor = currentEditor.getEditorComponent(); comboBox.revalidate(); } /** * Uninstalls components from this {@link JComboBox}. * * @see #installComponents() */ protected void uninstallComponents() { // uninstall arrow button unconfigureArrowButton(); comboBox.remove(arrowButton); arrowButton = null; listBox = null; popup = null; comboBox.setRenderer(null); // if the editor is not an instanceof UIResource, it was not set by the // UI delegate, so don't clear it... ComboBoxEditor currentEditor = comboBox.getEditor(); if (currentEditor instanceof UIResource) { comboBox.setEditor(null); editor = null; } } /** * Adds the current editor to the combo box. */ public void addEditor() { comboBox.add(editor); } /** * Removes the current editor from the combo box. */ public void removeEditor() { comboBox.remove(editor); } /** * Configures the editor for this combo box. */ protected void configureEditor() { editor.setFont(comboBox.getFont()); comboBox.getEditor().setItem(comboBox.getSelectedItem()); // FIXME: Need to implement. Set font and add listeners. } /** * Unconfigures the editor for this combo nox. This method is not implemented. */ protected void unconfigureEditor() { // FIXME: Need to implement } /** * Configures the arrow button. * * @see #configureArrowButton() */ public void configureArrowButton() { arrowButton.setEnabled(comboBox.isEnabled()); arrowButton.setFont(comboBox.getFont()); } /** * Unconfigures the arrow button. * * @see #configureArrowButton() * * @specnote The specification says this method is implementation specific * and should not be used or overridden. */ public void unconfigureArrowButton() { // Nothing to do here yet. } /** * Creates an arrow button for this {@link JComboBox}. The arrow button is * displayed at the right end of the combo box and is used to display/hide * the drop down list of items. * * @return A new button. */ protected JButton createArrowButton() { return new BasicArrowButton(BasicArrowButton.SOUTH, buttonBackground, buttonShadow, buttonDarkShadow, buttonHighlight); } /** * Returns <code>true</code> if the popup is visible, and <code>false</code> * otherwise. * * @param c The JComboBox to check * * @return <code>true</code> if popup part of the JComboBox is visible and * <code>false</code> otherwise. */ public boolean isPopupVisible(JComboBox c) { return popup.isVisible(); } /** * Displays/hides the {@link JComboBox}'s list of items on the screen. * * @param c The combo box, for which list of items should be * displayed/hidden * @param v true if show popup part of the jcomboBox and false to hide. */ public void setPopupVisible(JComboBox c, boolean v) { if (v) { popup.show(); popup.getList().requestFocus(); } else popup.hide(); } /** * JComboBox is focus traversable if it is editable and not otherwise. * * @param c combo box for which to check whether it is focus traversable * * @return true if focus tranversable and false otherwise */ public boolean isFocusTraversable(JComboBox c) { if (!comboBox.isEditable()) return true; return false; } /** * Paints given menu item using specified graphics context * * @param g The graphics context used to paint this combo box * @param c comboBox which needs to be painted. */ public void paint(Graphics g, JComponent c) { Rectangle rect = rectangleForCurrentValue(); paintCurrentValueBackground(g, rect, hasFocus); paintCurrentValue(g, rect, hasFocus); } /** * Returns preferred size for the combo box. * * @param c comboBox for which to get preferred size * * @return The preferred size for the given combo box */ public Dimension getPreferredSize(JComponent c) { // note: overriding getMinimumSize() (for example in the MetalComboBoxUI // class) affects the getPreferredSize() result, so it seems logical that // this method is implemented by delegating to the getMinimumSize() method return getMinimumSize(c); } /** * Returns the minimum size for this {@link JComboBox} for this * look and feel. * * @param c The {@link JComponent} to find the minimum size for. * * @return The dimensions of the minimum size. */ public Dimension getMinimumSize(JComponent c) { Dimension d = getDisplaySize(); int arrowButtonWidth = d.height; Dimension result = new Dimension(d.width + arrowButtonWidth, d.height); return result; } /** The value returned by the getMaximumSize() method. */ private static final Dimension MAXIMUM_SIZE = new Dimension(32767, 32767); /** * Returns the maximum size for this {@link JComboBox} for this * look and feel. * * @param c The {@link JComponent} to find the maximum size for * * @return The maximum size (<code>Dimension(32767, 32767)</code>). */ public Dimension getMaximumSize(JComponent c) { return MAXIMUM_SIZE; } public int getAccessibleChildrenCount(JComponent c) { // FIXME: Need to implement return 0; } public Accessible getAccessibleChild(JComponent c, int i) { // FIXME: Need to implement return null; } /** * Returns true if the specified key is a navigation key and false otherwise * * @param keyCode a key for which to check whether it is navigation key or * not. * * @return true if the specified key is a navigation key and false otherwis */ protected boolean isNavigationKey(int keyCode) { return false; } /** * Selects next possible item relative to the current selection * to be next selected item in the combo box. */ protected void selectNextPossibleValue() { int index = comboBox.getSelectedIndex(); if (index != comboBox.getItemCount() - 1) comboBox.setSelectedIndex(index + 1); } /** * Selects previous item relative to current selection to be * next selected item. */ protected void selectPreviousPossibleValue() { int index = comboBox.getSelectedIndex(); if (index != 0) comboBox.setSelectedIndex(index - 1); } /** * Displays combo box popup if the popup is not currently shown * on the screen and hides it if it is currently shown */ protected void toggleOpenClose() { setPopupVisible(comboBox, ! isPopupVisible(comboBox)); } /** * Returns the bounds in which comboBox's selected item will be * displayed. * * @return rectangle bounds in which comboBox's selected Item will be * displayed */ protected Rectangle rectangleForCurrentValue() { Rectangle cbBounds = SwingUtilities.getLocalBounds(comboBox); Rectangle abBounds = arrowButton.getBounds(); Rectangle rectForCurrentValue = new Rectangle(cbBounds.x, cbBounds.y, cbBounds.width - abBounds.width, cbBounds.height); return rectForCurrentValue; } /** * Returns the insets of the current border. * * @return Insets representing space between combo box and its border */ protected Insets getInsets() { return new Insets(0, 0, 0, 0); } /** * Paints currently selected value in the main part of the combo * box (part without popup). * * @param g graphics context * @param bounds Rectangle representing the size of the area in which * selected item should be drawn * @param hasFocus true if combo box has focus and false otherwise */ public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) { if (! comboBox.isEditable()) { Object currentValue = comboBox.getSelectedItem(); boolean isPressed = arrowButton.getModel().isPressed(); /* Gets the component to be drawn for the current value. * If there is currently no selected item we will take an empty * String as replacement. */ Component comp = comboBox.getRenderer().getListCellRendererComponent( listBox, (currentValue != null ? currentValue : ""), -1, isPressed, hasFocus); if (! comboBox.isEnabled()) { comp.setBackground(UIManager.getColor( "ComboBox.disabledBackground")); comp.setForeground(UIManager.getColor( "ComboBox.disabledForeground")); comp.setEnabled(false); } comp.setBounds(0, 0, bounds.width, bounds.height); comp.setFont(comboBox.getFont()); comp.paint(g); comboBox.revalidate(); } else comboBox.getEditor().setItem(comboBox.getSelectedItem()); } /** * Paints the background of part of the combo box, where currently * selected value is displayed. If the combo box has focus this method * should also paint focus rectangle around the combo box. * * @param g graphics context * @param bounds Rectangle representing the size of the largest item in the * comboBox * @param hasFocus true if combo box has fox and false otherwise */ public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus) { // background is painted by renderer, so it seems that nothing // should be done here. } /** * Returns the default size for the display area of a combo box that does * not contain any elements. This method returns the width and height of * a single space in the current font, plus a margin of 1 pixel. * * @return The default display size. * * @see #getDisplaySize() */ protected Dimension getDefaultSize() { // There is nothing in the spec to say how this method should be
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -