📄 basiccomboboxui.java
字号:
/** * Gets the insets from the JComboBox. */ protected Insets getInsets() { return comboBox.getInsets(); } // // end Utility Methods //==================== //=============================== // begin Painting Utility Methods // /** * Paints the currently selected item. */ public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) { ListCellRenderer renderer = comboBox.getRenderer(); Component c; if ( hasFocus && !isPopupVisible(comboBox) ) { c = renderer.getListCellRendererComponent( listBox, comboBox.getSelectedItem(), -1, true, false ); } else { c = renderer.getListCellRendererComponent( listBox, comboBox.getSelectedItem(), -1, false, false ); c.setBackground(UIManager.getColor("ComboBox.background")); } c.setFont(comboBox.getFont()); if ( hasFocus && !isPopupVisible(comboBox) ) { c.setForeground(listBox.getSelectionForeground()); c.setBackground(listBox.getSelectionBackground()); } else { if ( comboBox.isEnabled() ) { c.setForeground(comboBox.getForeground()); c.setBackground(comboBox.getBackground()); } else { c.setForeground(DefaultLookup.getColor( comboBox, this, "ComboBox.disabledForeground", null)); c.setBackground(DefaultLookup.getColor( comboBox, this, "ComboBox.disabledBackground", null)); } } // Fix for 4238829: should lay out the JPanel. boolean shouldValidate = false; if (c instanceof JPanel) { shouldValidate = true; } currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y, bounds.width,bounds.height, shouldValidate); } /** * Paints the background of the currently selected item. */ public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus) { Color t = g.getColor(); if ( comboBox.isEnabled() ) g.setColor(DefaultLookup.getColor(comboBox, this, "ComboBox.background", null)); else g.setColor(DefaultLookup.getColor(comboBox, this, "ComboBox.disabledBackground", null)); g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height); g.setColor(t); } /** * Repaint the currently selected item. */ void repaintCurrentValue() { Rectangle r = rectangleForCurrentValue(); comboBox.repaint(r.x,r.y,r.width,r.height); } // // end Painting Utility Methods //============================= //=============================== // begin Size Utility Methods // /** * Return the default size of an empty display area of the combo box using * the current renderer and font. * * @return the size of an empty display area * @see #getDisplaySize */ protected Dimension getDefaultSize() { // Calculates the height and width using the default text renderer Dimension d = getSizeForComponent(getDefaultListCellRenderer().getListCellRendererComponent(listBox, " ", -1, false, false)); return new Dimension(d.width, d.height); } /** * Returns the calculated size of the display area. The display area is the * portion of the combo box in which the selected item is displayed. This * method will use the prototype display value if it has been set. * <p> * For combo boxes with a non trivial number of items, it is recommended to * use a prototype display value to significantly speed up the display * size calculation. * * @return the size of the display area calculated from the combo box items * @see javax.swing.JComboBox#setPrototypeDisplayValue */ protected Dimension getDisplaySize() { if (!isDisplaySizeDirty) { return new Dimension(cachedDisplaySize); } Dimension result = new Dimension(); ListCellRenderer renderer = comboBox.getRenderer(); if (renderer == null) { renderer = new DefaultListCellRenderer(); } sameBaseline = true; Object prototypeValue = comboBox.getPrototypeDisplayValue(); if (prototypeValue != null) { // Calculates the dimension based on the prototype value result = getSizeForComponent(renderer.getListCellRendererComponent(listBox, prototypeValue, -1, false, false)); } else { // Calculate the dimension by iterating over all the elements in the combo // box list. ComboBoxModel model = comboBox.getModel(); int modelSize = model.getSize(); int baseline = -1; Dimension d; Component cpn; if (modelSize > 0 ) { for (int i = 0; i < modelSize ; i++ ) { // Calculates the maximum height and width based on the largest // element Object value = model.getElementAt(i); Component c = renderer.getListCellRendererComponent( listBox, value, -1, false, false); d = getSizeForComponent(c); if (sameBaseline && value != null && (!(value instanceof String) || !"".equals(value))) { int newBaseline = c.getBaseline(d.width, d.height); if (newBaseline == -1) { sameBaseline = false; } else if (baseline == -1) { baseline = newBaseline; } else if (baseline != newBaseline) { sameBaseline = false; } } result.width = Math.max(result.width,d.width); result.height = Math.max(result.height,d.height); } } else { result = getDefaultSize(); if (comboBox.isEditable()) { result.width = 100; } } } if ( comboBox.isEditable() ) { Dimension d = editor.getPreferredSize(); result.width = Math.max(result.width,d.width); result.height = Math.max(result.height,d.height); } // Set the cached value cachedDisplaySize.setSize(result.width, result.height); isDisplaySizeDirty = false; return result; } /** * This has been refactored out in hopes that it may be investigated and * simplified for the next major release. adding/removing * the component to the currentValuePane and changing the font may be * redundant operations. */ private Dimension getSizeForComponent(Component comp) { currentValuePane.add(comp); comp.setFont(comboBox.getFont()); Dimension d = comp.getPreferredSize(); currentValuePane.remove(comp); return d; } // // end Size Utility Methods //============================= //================================= // begin Keyboard Action Management // /** * Adds keyboard actions to the JComboBox. Actions on enter and esc are already * supplied. Add more actions as you need them. */ protected void installKeyboardActions() { InputMap km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(comboBox, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); LazyActionMap.installLazyActionMap(comboBox, BasicComboBoxUI.class, "ComboBox.actionMap"); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)DefaultLookup.get(comboBox, this, "ComboBox.ancestorInputMap"); } return null; } boolean isTableCellEditor() { return isTableCellEditor; } /** * Removes the focus InputMap and ActionMap. */ protected void uninstallKeyboardActions() { SwingUtilities.replaceUIInputMap(comboBox, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIActionMap(comboBox, null); } // // Actions // private static class Actions extends UIAction { private static final String HIDE = "hidePopup"; private static final String DOWN = "selectNext"; private static final String DOWN_2 = "selectNext2"; private static final String TOGGLE = "togglePopup"; private static final String TOGGLE_2 = "spacePopup"; private static final String UP = "selectPrevious"; private static final String UP_2 = "selectPrevious2"; private static final String ENTER = "enterPressed"; private static final String PAGE_DOWN = "pageDownPassThrough"; private static final String PAGE_UP = "pageUpPassThrough"; private static final String HOME = "homePassThrough"; private static final String END = "endPassThrough"; Actions(String name) { super(name); } public void actionPerformed( ActionEvent e ) { String key = getName(); JComboBox comboBox = (JComboBox)e.getSource(); BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType( comboBox.getUI(), BasicComboBoxUI.class); if (key == HIDE) { comboBox.firePopupMenuCanceled(); comboBox.setPopupVisible(false); } else if (key == PAGE_DOWN || key == PAGE_UP || key == HOME || key == END) { int index = getNextIndex(comboBox, key); if (index >= 0 && index < comboBox.getItemCount()) { comboBox.setSelectedIndex(index); } } else if (key == DOWN) { if (comboBox.isShowing() ) { if ( comboBox.isPopupVisible() ) { if (ui != null) { ui.selectNextPossibleValue(); } } else { comboBox.setPopupVisible(true); } } } else if (key == DOWN_2) { // Special case in which pressing the arrow keys will not // make the popup appear - except for editable combo boxes // and combo boxes inside a table. if (comboBox.isShowing() ) { if ( (comboBox.isEditable() || (ui != null && ui.isTableCellEditor())) && !comboBox.isPopupVisible() ) { comboBox.setPopupVisible(true); } else { if (ui != null) { ui.selectNextPossibleValue(); } } } } else if (key == TOGGLE || key == TOGGLE_2) { if (ui != null && (key == TOGGLE || !comboBox.isEditable())) { if ( ui.isTableCellEditor() ) { // Forces the selection of the list item if the // combo box is in a JTable. comboBox.setSelectedIndex(ui.popup.getList(). getSelectedIndex()); } else { comboBox.setPopupVisible(!comboBox.isPopupVisible()); } } } else if (key == UP) { if (ui != null) { if (ui.isPopupVisible(comboBox)) { ui.selectPreviousPossibleValue(); } else if (DefaultLookup.getBoolean(comboBox, ui, "ComboBox.showPopupOnNavigation", false)) { ui.setPopupVisible(comboBox, true); } } } else if (key == UP_2) { // Special case in which pressing the arrow keys will not // make the popup appear - except for editable combo boxes. if (comboBox.isShowing() && ui != null) { if ( comboBox.isEditable() && !comboBox.isPopupVisible()) { comboBox.setPopupVisible(true); } else { ui.selectPreviousPossibleValue(); } } } else if (key == ENTER) { if (comboBox.isPopupVisible()) { // Forces the selection of the list item boolean isEnterSelectablePopup = UIManager.getBoolean("ComboBox.isEnterSelectablePopup"); if (!comboBox.isEditable() || isEnterSelectablePopup) { Object listItem = ui.popup.getList().getSelectedValue(); if (listItem != null) { comboBox.getModel().setSelectedItem(listItem); // Ensure that JComboBox.actionPerformed() // doesn't set editor value as selected item comboBox.getEditor().setItem(listItem); } } comboBox.setPopupVisible(false); } else { // Call the default button binding.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -