public void layoutContainer(Container parent) { JComboBox cb = (JComboBox) parent; // Use superclass behavior if the combobox is not editable. if (!cb.isEditable()) { super.layoutContainer(parent); return; } int width = cb.getWidth(); int height = cb.getHeight(); Insets insets = getInsets(); int buttonWidth = getEditableButtonWidth(); int buttonHeight = height - (insets.top + insets.bottom); if (arrowButton != null) { if (cb.getComponentOrientation().isLeftToRight()) { arrowButton.setBounds( width - (insets.right + buttonWidth), insets.top, buttonWidth, buttonHeight); } else { arrowButton.setBounds(insets.left, insets.top, buttonWidth, buttonHeight); } } if (editor != null) { editor.setBounds(rectangleForCurrentValue()); } }
private static int getComboBoxBaselineResizeBehavior(JComboBox cb) { if (cb.isEditable()) { return getBaselineResizeBehavior(cb.getEditor().getEditorComponent()); } ListCellRenderer renderer = cb.getRenderer(); if (renderer == null) { if (brbListCellRenderer == null) { brbListCellRenderer = new DefaultListCellRenderer(); } renderer = brbListCellRenderer; } Object value = null; Object prototypeValue = cb.getPrototypeDisplayValue(); if (prototypeValue != null) { value = prototypeValue; } else if (cb.getModel().getSize() > 0) { value = cb.getModel().getElementAt(0); } if (value != null) { if (brbList == null) { brbList = new JList(); } Component component = renderer.getListCellRendererComponent(brbList, value, -1, false, false); return getBaselineResizeBehavior(component); } return BRB_OTHER; }
@Override public void keyPressed(KeyEvent evt) { if (evt.isConsumed()) return; Component comp = getFocusOwner(); if (evt.getKeyCode() == KeyEvent.VK_ENTER && enterEnabled) { while (comp != null) { if (comp instanceof JComboBox) { JComboBox<?> combo = (JComboBox<?>) comp; if (combo.isEditable()) { Object selected = combo.getEditor().getItem(); if (selected != null) combo.setSelectedItem(selected); } if (combo.isPopupVisible()) { evt.consume(); combo.setPopupVisible(false); } return; } // TODO: add other classes that need custom key handling here. comp = comp.getParent(); } evt.consume(); ok(); } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE || isCloseBufferShortcut(evt)) { evt.consume(); if (comp instanceof JComboBox) { JComboBox<?> combo = (JComboBox<?>) comp; if (combo.isPopupVisible()) combo.setPopupVisible(false); else cancel(); } else cancel(); } }
private static int getComboBoxBaseline(JComboBox combobox, int height) { Insets insets = combobox.getInsets(); int y = insets.top; height -= (insets.top + insets.bottom); if (combobox.isEditable()) { ComboBoxEditor editor = combobox.getEditor(); if (editor != null && (editor.getEditorComponent() instanceof JTextField)) { JTextField tf = (JTextField) editor.getEditorComponent(); return y + getSingleLineTextBaseline(tf, height); } } // Use the renderer to calculate baseline if (isMetal()) { if (isOceanTheme()) { y += 2; height -= 4; } } else if (isWindows()) { // This doesn't guarantee an XP style will be active, // but we don't offer public API to detect if XP is active. String osVersion = System.getProperty("os.version"); if (osVersion != null) { Float version = Float.valueOf(osVersion); if (version.floatValue() > 4.0) { y += 2; height -= 4; } } } ListCellRenderer renderer = combobox.getRenderer(); if (renderer instanceof JLabel) { int baseline = y + getLabelBaseline((JLabel) renderer, height); if (isAqua()) { return baseline - 1; } return baseline; } // Renderer isn't a label, use metrics directly. FontMetrics fm = combobox.getFontMetrics(combobox.getFont()); return y + fm.getAscent(); }