📄 basiccomboboxui.java
字号:
// implemented...so I've done some guessing, written some Mauve tests, // and written something that gives dimensions that are close to the // reference implementation. FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont()); int w = fm.charWidth(' ') + 2; int h = fm.getHeight() + 2; return new Dimension(w, h); } /** * Returns the size of the display area for the combo box. This size will be * the size of the combo box, not including the arrowButton. * * @return The size of the display area for the combo box. */ protected Dimension getDisplaySize() { if (!comboBox.isEditable()) { Object prototype = comboBox.getPrototypeDisplayValue(); if (prototype != null) { // calculate result based on prototype ListCellRenderer renderer = comboBox.getRenderer(); Component comp = renderer.getListCellRendererComponent(listBox, prototype, -1, false, false); Dimension compSize = comp.getPreferredSize(); compSize.width += 2; // add 1 pixel margin around area compSize.height += 2; return compSize; } else { ComboBoxModel model = comboBox.getModel(); int numItems = model.getSize(); // if combo box doesn't have any items then simply // return its default size if (numItems == 0) { displaySize = getDefaultSize(); return displaySize; } Dimension size = new Dimension(0, 0); // ComboBox's display size should be equal to the // size of the largest item in the combo box. ListCellRenderer renderer = comboBox.getRenderer(); for (int i = 0; i < numItems; i++) { Object item = model.getElementAt(i); Component comp = renderer.getListCellRendererComponent(listBox, item, -1, false, false); Dimension compSize = comp.getPreferredSize(); if (compSize.width + 2 > size.width) size.width = compSize.width + 2; if (compSize.height + 2 > size.height) size.height = compSize.height + 2; } displaySize = size; return displaySize; } } else // an editable combo, { Component comp = comboBox.getEditor().getEditorComponent(); Dimension prefSize = comp.getPreferredSize(); int width = prefSize.width; int height = prefSize.height + 2; Object prototype = comboBox.getPrototypeDisplayValue(); if (prototype != null) { FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont()); width = Math.max(width, fm.stringWidth(prototype.toString()) + 2); } displaySize = new Dimension(width, height); return displaySize; } } /** * Installs the keyboard actions for the {@link JComboBox} as specified * by the look and feel. */ protected void installKeyboardActions() { // FIXME: Need to implement. } /** * Uninstalls the keyboard actions for the {@link JComboBox} there were * installed by in {@link #installListeners}. */ protected void uninstallKeyboardActions() { // FIXME: Need to implement. } /** * A {@link LayoutManager} used to position the sub-components of the * {@link JComboBox}. * * @see BasicComboBoxUI#createLayoutManager() */ public class ComboBoxLayoutManager implements LayoutManager { /** * Creates a new ComboBoxLayoutManager object. */ public ComboBoxLayoutManager() { // Nothing to do here. } /** * Adds a component to the layout. This method does nothing, since the * layout manager doesn't need to track the components. * * @param name the name to associate the component with (ignored). * @param comp the component (ignored). */ public void addLayoutComponent(String name, Component comp) { // Do nothing } /** * Removes a component from the layout. This method does nothing, since * the layout manager doesn't need to track the components. * * @param comp the component. */ public void removeLayoutComponent(Component comp) { // Do nothing } /** * Returns preferred layout size of the JComboBox. * * @param parent the Container for which the preferred size should be * calculated. * * @return The preferred size for the given container */ public Dimension preferredLayoutSize(Container parent) { return getPreferredSize((JComponent) parent); } /** * Returns the minimum layout size. * * @param parent the container. * * @return The minimum size. */ public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } /** * Arranges the components in the container. It puts arrow * button right end part of the comboBox. If the comboBox is editable * then editor is placed to the left of arrow button, starting from the * beginning. * * @param parent Container that should be layed out. */ public void layoutContainer(Container parent) { // Position editor component to the left of arrow button if combo box is // editable int arrowSize = comboBox.getHeight(); int editorWidth = comboBox.getBounds().width - arrowSize; if (comboBox.isEditable()) editor.setBounds(0, 0, editorWidth, comboBox.getBounds().height); arrowButton.setBounds(editorWidth, 0, arrowSize, arrowSize); comboBox.revalidate(); } } /** * Handles focus changes occuring in the combo box. This class is * responsible for repainting combo box whenever focus is gained or lost * and also for hiding popup list of items whenever combo box loses its * focus. */ public class FocusHandler extends Object implements FocusListener { /** * Creates a new FocusHandler object. */ public FocusHandler() { // Nothing to do here. } /** * Invoked when combo box gains focus. It repaints main * part of combo box accordingly. * * @param e the FocusEvent */ public void focusGained(FocusEvent e) { hasFocus = true; comboBox.repaint(); } /** * Invoked when the combo box loses focus. It repaints the main part * of the combo box accordingly and hides the popup list of items. * * @param e the FocusEvent */ public void focusLost(FocusEvent e) { hasFocus = false; setPopupVisible(comboBox, false); comboBox.repaint(); } } /** * Handles {@link ItemEvent}s fired by the {@link JComboBox} when its * selected item changes. */ public class ItemHandler extends Object implements ItemListener { /** * Creates a new ItemHandler object. */ public ItemHandler() { // Nothing to do here. } /** * Invoked when selected item becomes deselected or when * new item becomes selected. * * @param e the ItemEvent representing item's state change. */ public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED && comboBox.isEditable()) comboBox.getEditor().setItem(e.getItem()); comboBox.repaint(); } } /** * KeyHandler handles key events occuring while JComboBox has focus. */ public class KeyHandler extends KeyAdapter { public KeyHandler() { // Nothing to do here. } /** * Invoked whenever key is pressed while JComboBox is in focus. */ public void keyPressed(KeyEvent e) { // FIXME: This method calls JComboBox.selectWithKeyChar if the key that was // pressed is not a navigation key. } } /** * Handles the changes occurring in the JComboBox's data model. */ public class ListDataHandler extends Object implements ListDataListener { /** * Creates a new ListDataHandler object. */ public ListDataHandler() { // Nothing to do here. } /** * Invoked if the content's of JComboBox's data model are changed. * * @param e ListDataEvent describing the change. */ public void contentsChanged(ListDataEvent e) { // if the item is selected or deselected } /** * Invoked when items are added to the JComboBox's data model. * * @param e ListDataEvent describing the change. */ public void intervalAdded(ListDataEvent e) { ComboBoxModel model = comboBox.getModel(); ListCellRenderer renderer = comboBox.getRenderer(); if (displaySize == null) displaySize = getDisplaySize(); if (displaySize.width < getDefaultSize().width) displaySize.width = getDefaultSize().width; if (displaySize.height < getDefaultSize().height) displaySize.height = getDefaultSize().height; comboBox.repaint(); } /** * Invoked when items are removed from the JComboBox's * data model. * * @param e ListDataEvent describing the change. */ public void intervalRemoved(ListDataEvent e) { // recalculate display size of the JComboBox. displaySize = getDisplaySize(); comboBox.repaint(); } } /** * Handles {@link PropertyChangeEvent}s fired by the {@link JComboBox}. */ public class PropertyChangeHandler extends Object implements PropertyChangeListener { /** * Creates a new instance. */ public PropertyChangeHandler() { // Nothing to do here. } /** * Invoked whenever bound property of JComboBox changes. * * @param e the event. */ public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals("enabled")) { arrowButton.setEnabled(comboBox.isEnabled()); if (comboBox.isEditable()) comboBox.getEditor().getEditorComponent().setEnabled(comboBox .isEnabled()); } else if (e.getPropertyName().equals("editable")) { if (comboBox.isEditable()) { configureEditor(); addEditor(); } else { unconfigureEditor(); removeEditor(); } comboBox.revalidate(); comboBox.repaint(); } else if (e.getPropertyName().equals("dataModel")) { // remove ListDataListener from old model and add it to new model ComboBoxModel oldModel = (ComboBoxModel) e.getOldValue(); if (oldModel != null) oldModel.removeListDataListener(listDataListener); if ((ComboBoxModel) e.getNewValue() != null) comboBox.getModel().addListDataListener(listDataListener); } else if (e.getPropertyName().equals("font")) { Font font = (Font) e.getNewValue(); editor.setFont(font); listBox.setFont(font); arrowButton.setFont(font); comboBox.revalidate(); comboBox.repaint(); } // FIXME: Need to handle changes in other bound properties. } } /** * A handler for mouse events occurring in the combo box. An instance of * this class is returned by the <code>createMouseListener()</code> method. */ private class MouseHandler extends MouseAdapter { /** * Invoked when mouse is pressed over the combo box. It toggles the * visibility of the popup list. * * @param e the event */ public void mousePressed(MouseEvent e) { if (comboBox.isEnabled()) toggleOpenClose(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -