basiccomboboxui.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,239 行 · 第 1/3 页
JAVA
1,239 行
} /** * Returns default size for the combo box that doesn't contain any elements * in it * * @return Default size of the combo box with no elements in it. */ protected Dimension getDefaultSize() { return new Dimension(6, 17); } /** * Returns size of the largest item in the combo box. This size will be the * size of the combo box, not including the arrowButton. * * @return dimensions of the largest item in the combo box. */ protected Dimension getLargestItemSize() { 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) { largestItemSize = getDefaultSize(); return largestItemSize; } 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); String s = item.toString(); Component comp = renderer.getListCellRendererComponent(listBox, item, -1, false, false); if (comp.getPreferredSize().getWidth() > size.getWidth()) size = comp.getPreferredSize(); } largestItemSize = size; return largestItemSize; } /** * This method installs the keyboard actions for the JComboBox as specified * by the look and feel. */ protected void installKeyboardActions() { // FIXME: Need to implement. } /** * This method uninstalls the keyboard actions for the JComboBox there were * installed by in {@link #installListeners}. */ protected void uninstallKeyboardActions() { // FIXME: Need to implement. } /** * This class is Layout Manager for this combo box. */ public class ComboBoxLayoutManager extends Object implements LayoutManager { /** * Creates a new ComboBoxLayoutManager object. */ public ComboBoxLayoutManager() { } public void addLayoutComponent(String name, Component comp) { // Do nothing } public void removeLayoutComponent(Component comp) { // Do nothing } /** * Returns preferred layout size of the JComboBox. * * @param parent Container for which preferred size should be calculated * * @return preferred size for the given container */ public Dimension preferredLayoutSize(Container parent) { Dimension d = new Dimension(0, 0); if (largestItemSize == null) largestItemSize = getLargestItemSize(); // add size for the area that will display selected item d.width += largestItemSize.getWidth(); d.height += largestItemSize.getHeight(); // add size of the arrow button d.width += arrowButtonWidth; // add width and height of the border d.width += borderInsets.left + borderInsets.right; d.height += borderInsets.left + borderInsets.right; // Add combo box's insets Insets insets = parent.getInsets(); d.width += insets.left + insets.right; d.width += insets.left + insets.right; return d; } public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } /** * This method layouts out 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 editorWidth = comboBox.getBounds().width - arrowButtonWidth - 2; if (comboBox.isEditable()) editor.setBounds(borderInsets.left, borderInsets.top, editorWidth, comboBox.getBounds().height - borderInsets.left - borderInsets.top); arrowButton.setBounds(editorWidth, 2, arrowButtonWidth, comboBox.getBounds().height - 4); comboBox.revalidate(); } } /** * This class 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() { } /** * This mehtod is invoked when combo box gains focus. It repaints main * part of combo box accordingally. * * @param e the FocusEvent */ public void focusGained(FocusEvent e) { hasFocus = true; comboBox.repaint(); } /** * This method is invoked when combo box loses focus It repaint main part * of combo box accordingally and hides popup list of items. * * @param e the FocusEvent */ public void focusLost(FocusEvent e) { hasFocus = false; comboBox.repaint(); popup.hide(); } } /** * This class handles ItemEvent fired by the JComboBox when its selected * item changes. */ public class ItemHandler extends Object implements ItemListener { /** * Creates a new ItemHandler object. */ public ItemHandler() { } /** * This method is 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) { comboBox.repaint(); } } /** * KeyHandler handles key events occuring while JComboBox has focus. */ public class KeyHandler extends KeyAdapter { public KeyHandler() { } /* * This method is 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. } } /** * This class handles to the changes occuring in the JComboBox's data model */ public class ListDataHandler extends Object implements ListDataListener { /** * Creates a new ListDataHandler object. */ public ListDataHandler() { } /** * This method is invoked 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 } /** * This method is invoked when items were added to the JComboBox's data * model. * * @param e ListDataEvent describing the change. */ public void intervalAdded(ListDataEvent e) { // must determine if the size of the combo box should change int start = e.getIndex0(); int end = e.getIndex1(); ComboBoxModel model = comboBox.getModel(); ListCellRenderer renderer = comboBox.getRenderer(); if (largestItemSize == null) largestItemSize = new Dimension(0, 0); for (int i = start - 1; i < end; i++) { Object item = model.getElementAt(i); Component comp = renderer.getListCellRendererComponent(new JList(), item, -1, false, false); if (comp.getPreferredSize().getWidth() > largestItemSize.getWidth()) largestItemSize = comp.getPreferredSize(); } } /** * This method is invoked when items were removed from the JComboBox's * data model. * * @param e ListDataEvent describing the change. */ public void intervalRemoved(ListDataEvent e) { // recalculate display size of the JComboBox. largestItemSize = getLargestItemSize(); comboBox.repaint(); } } /** * This class handles PropertyChangeEvents fired by JComboBox. */ public class PropertyChangeHandler extends Object implements PropertyChangeListener { public PropertyChangeHandler() { } /** * This method is invoked whenever bound property of JComboBox changes. */ 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); } // FIXME: Need to handle changes in other bound properties. } } /** * MouseHandler listens to mouse events occuring in the combo box. This * class is responsible for repainting this JComboBox whenever the mouse is * being pressed or released over it. */ private class MouseHandler extends MouseAdapter { /** * This method is invoked when mouse is pressed over the combo box. It * repaints the combo box accordinglly * * @param e the MouseEvent */ public void mousePressed(MouseEvent e) { if (comboBox.isEnabled()) { if (e.getSource() instanceof JComboBox) { arrowButton.getModel().setPressed(true); arrowButton.getModel().setArmed(true); } comboBox.repaint(); if (e.getSource() instanceof BasicArrowButton) toggleOpenClose(); } } /** * This method is invoked when mouse is released over the combo box. It * repaints the combo box accordinglly * * @param e the MouseEvent */ public void mouseReleased(MouseEvent e) { if (comboBox.isEnabled()) { if (e.getSource() instanceof JComboBox) { arrowButton.getModel().setPressed(false); arrowButton.getModel().setArmed(false); } comboBox.repaint(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?