| Method from javax.swing.plaf.basic.BasicComboBoxUI Detail: |
public void addEditor() {
removeEditor();
editor = comboBox.getEditor().getEditorComponent();
if ( editor != null ) {
configureEditor();
comboBox.add(editor);
if(comboBox.isFocusOwner()) {
// Switch focus to the editor component
editor.requestFocusInWindow();
}
}
}
This public method is implementation specific and should be private.
do not call or override. To implement a specific editor create a
custom ComboBoxEditor |
public void configureArrowButton() {
if ( arrowButton != null ) {
arrowButton.setEnabled( comboBox.isEnabled() );
arrowButton.setFocusable(comboBox.isFocusable());
arrowButton.setRequestFocusEnabled(false);
arrowButton.addMouseListener( popup.getMouseListener() );
arrowButton.addMouseMotionListener( popup.getMouseMotionListener() );
arrowButton.resetKeyboardActions();
arrowButton.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY);
arrowButton.setInheritsPopupMenu(true);
}
}
This public method is implementation specific and should be private. Do
not call or override. |
protected void configureEditor() {
// Should be in the same state as the combobox
editor.setEnabled(comboBox.isEnabled());
editor.setFocusable(comboBox.isFocusable());
editor.setFont( comboBox.getFont() );
if (focusListener != null) {
editor.addFocusListener(focusListener);
}
editor.addFocusListener( getHandler() );
comboBox.getEditor().addActionListener(getHandler());
if(editor instanceof JComponent) {
((JComponent)editor).putClientProperty("doNotCancelPopup",
HIDE_POPUP_KEY);
((JComponent)editor).setInheritsPopupMenu(true);
}
comboBox.configureEditor(comboBox.getEditor(),comboBox.getSelectedItem());
}
This protected method is implementation specific and should be private.
do not call or override. |
protected JButton createArrowButton() {
JButton button = new BasicArrowButton(BasicArrowButton.SOUTH,
UIManager.getColor("ComboBox.buttonBackground"),
UIManager.getColor("ComboBox.buttonShadow"),
UIManager.getColor("ComboBox.buttonDarkShadow"),
UIManager.getColor("ComboBox.buttonHighlight"));
button.setName("ComboBox.arrowButton");
return button;
}
Creates an button which will be used as the control to show or hide
the popup portion of the combo box. |
protected ComboBoxEditor createEditor() {
return new BasicComboBoxEditor.UIResource();
}
Creates the default editor that will be used in editable combo boxes.
A default editor will be used only if an editor has not been
explicitly set with setEditor. |
protected FocusListener createFocusListener() {
return getHandler();
}
Creates a FocusListener which will be added to the combo box.
If this method returns null then it will not be added to the combo box. |
protected ItemListener createItemListener() {
return null;
}
|
protected KeyListener createKeyListener() {
return getHandler();
}
Creates a KeyListener which will be added to the
combo box. If this method returns null then it will not be added
to the combo box. |
protected LayoutManager createLayoutManager() {
return getHandler();
}
Creates a layout manager for managing the components which make up the
combo box. |
protected ListDataListener createListDataListener() {
return getHandler();
}
Creates a list data listener which will be added to the
ComboBoxModel. If this method returns null then
it will not be added to the combo box model. |
protected ComboPopup createPopup() {
return new BasicComboPopup( comboBox );
}
Creates the popup portion of the combo box. |
protected PropertyChangeListener createPropertyChangeListener() {
return getHandler();
}
Creates a PropertyChangeListener which will be added to
the combo box. If this method returns null then it will not
be added to the combo box. |
protected ListCellRenderer createRenderer() {
return new BasicComboBoxRenderer.UIResource();
}
Creates the default renderer that will be used in a non-editiable combo
box. A default renderer will used only if a renderer has not been
explicitly set with setRenderer. |
public static ComponentUI createUI(JComponent c) {
return new BasicComboBoxUI();
}
|
public Accessible getAccessibleChild(JComponent c,
int i) {
// 0 = the popup
// 1 = the editor
switch ( i ) {
case 0:
if ( popup instanceof Accessible ) {
AccessibleContext ac = ((Accessible) popup).getAccessibleContext();
ac.setAccessibleParent(comboBox);
return(Accessible) popup;
}
break;
case 1:
if ( comboBox.isEditable()
&& (editor instanceof Accessible) ) {
AccessibleContext ac = ((Accessible) editor).getAccessibleContext();
ac.setAccessibleParent(comboBox);
return(Accessible) editor;
}
break;
}
return null;
}
|
public int getAccessibleChildrenCount(JComponent c) {
if ( comboBox.isEditable() ) {
return 2;
}
else {
return 1;
}
}
|
public int getBaseline(JComponent c,
int width,
int height) {
super.getBaseline(c, width, height);
int baseline = -1;
// force sameBaseline to be updated.
getDisplaySize();
if (sameBaseline) {
Insets insets = c.getInsets();
height = height - insets.top - insets.bottom;
if (!comboBox.isEditable()) {
ListCellRenderer renderer = comboBox.getRenderer();
if (renderer == null) {
renderer = new DefaultListCellRenderer();
}
Object value = null;
Object prototypeValue = comboBox.getPrototypeDisplayValue();
if (prototypeValue != null) {
value = prototypeValue;
}
else if (comboBox.getModel().getSize() > 0) {
// Note, we're assuming the baseline is the same for all
// cells, if not, this needs to loop through all.
value = comboBox.getModel().getElementAt(0);
}
if (value == null) {
value = " ";
} else if (value instanceof String && "".equals(value)) {
value = " ";
}
Component component = renderer.
getListCellRendererComponent(listBox, value, -1,
false, false);
if (component instanceof JComponent) {
component.setFont(comboBox.getFont());
}
baseline = component.getBaseline(width, height);
}
else {
baseline = editor.getBaseline(width, height);
}
if (baseline > 0) {
baseline += insets.top;
}
}
return baseline;
}
|
public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c) {
super.getBaselineResizeBehavior(c);
// Force sameBaseline to be updated.
getDisplaySize();
if (comboBox.isEditable()) {
return editor.getBaselineResizeBehavior();
}
else if (sameBaseline) {
ListCellRenderer renderer = comboBox.getRenderer();
if (renderer == null) {
renderer = new DefaultListCellRenderer();
}
Object value = null;
Object prototypeValue = comboBox.getPrototypeDisplayValue();
if (prototypeValue != null) {
value = prototypeValue;
}
else if (comboBox.getModel().getSize() > 0) {
// Note, we're assuming the baseline is the same for all
// cells, if not, this needs to loop through all.
value = comboBox.getModel().getElementAt(0);
}
if (value != null) {
Component component = renderer.
getListCellRendererComponent(listBox, value, -1,
false, false);
return component.getBaselineResizeBehavior();
}
}
return Component.BaselineResizeBehavior.OTHER;
}
Returns an enum indicating how the baseline of the component
changes as the size changes. |
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);
}
Return the default size of an empty display area of the combo box using
the current renderer and font. |
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;
}
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.
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. |
InputMap getInputMap(int condition) {
if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
return (InputMap)DefaultLookup.get(comboBox, this,
"ComboBox.ancestorInputMap");
}
return null;
}
|
protected Insets getInsets() {
return comboBox.getInsets();
}
Gets the insets from the JComboBox. |
public Dimension getMaximumSize(JComponent c) {
return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
}
|
public Dimension getMinimumSize(JComponent c) {
if ( !isMinimumSizeDirty ) {
return new Dimension(cachedMinimumSize);
}
Dimension size = getDisplaySize();
Insets insets = getInsets();
size.height += insets.top + insets.bottom;
int buttonSize = size.height - (insets.top + insets.bottom);
size.width += insets.left + insets.right + buttonSize;
cachedMinimumSize.setSize( size.width, size.height );
isMinimumSizeDirty = false;
return new Dimension(size);
}
The minumum size is the size of the display area plus insets plus the button. |
public Dimension getPreferredSize(JComponent c) {
return getMinimumSize(c);
}
|
protected void installComponents() {
arrowButton = createArrowButton();
comboBox.add( arrowButton );
if (arrowButton != null) {
configureArrowButton();
}
if ( comboBox.isEditable() ) {
addEditor();
}
comboBox.add( currentValuePane );
}
Creates and initializes the components which make up the
aggregate combo box. This method is called as part of the UI
installation process. |
protected void installDefaults() {
LookAndFeel.installColorsAndFont( comboBox,
"ComboBox.background",
"ComboBox.foreground",
"ComboBox.font" );
LookAndFeel.installBorder( comboBox, "ComboBox.border" );
LookAndFeel.installProperty( comboBox, "opaque", Boolean.TRUE);
Long l = (Long)UIManager.get("ComboBox.timeFactor");
timeFactor = (l!=null) ? l.longValue() : 1000L;
}
Installs the default colors, default font, default renderer, and default
editor into the JComboBox. |
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");
}
Adds keyboard actions to the JComboBox. Actions on enter and esc are already
supplied. Add more actions as you need them. |
protected void installListeners() {
if ( (itemListener = createItemListener()) != null) {
comboBox.addItemListener( itemListener );
}
if ( (propertyChangeListener = createPropertyChangeListener()) != null ) {
comboBox.addPropertyChangeListener( propertyChangeListener );
}
if ( (keyListener = createKeyListener()) != null ) {
comboBox.addKeyListener( keyListener );
}
if ( (focusListener = createFocusListener()) != null ) {
comboBox.addFocusListener( focusListener );
}
if ((popupMouseListener = popup.getMouseListener()) != null) {
comboBox.addMouseListener( popupMouseListener );
}
if ((popupMouseMotionListener = popup.getMouseMotionListener()) != null) {
comboBox.addMouseMotionListener( popupMouseMotionListener );
}
if ((popupKeyListener = popup.getKeyListener()) != null) {
comboBox.addKeyListener(popupKeyListener);
}
if ( comboBox.getModel() != null ) {
if ( (listDataListener = createListDataListener()) != null ) {
comboBox.getModel().addListDataListener( listDataListener );
}
}
}
Create and install the listeners for the combo box and its model.
This method is called when the UI is installed. |
public void installUI(JComponent c) {
isMinimumSizeDirty = true;
comboBox = (JComboBox)c;
installDefaults();
popup = createPopup();
listBox = popup.getList();
// Is this combo box a cell editor?
Boolean inTable = (Boolean)c.getClientProperty(IS_TABLE_CELL_EDITOR );
if (inTable != null) {
isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;
}
if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) {
comboBox.setRenderer( createRenderer() );
}
if ( comboBox.getEditor() == null || comboBox.getEditor() instanceof UIResource ) {
comboBox.setEditor( createEditor() );
}
installListeners();
installComponents();
comboBox.setLayout( createLayoutManager() );
comboBox.setRequestFocusEnabled( true );
installKeyboardActions();
comboBox.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY);
if (keySelectionManager == null || keySelectionManager instanceof UIResource) {
keySelectionManager = new DefaultKeySelectionManager();
}
comboBox.setKeySelectionManager(keySelectionManager);
}
|
public boolean isFocusTraversable(JComboBox c) {
return !comboBox.isEditable();
}
Determines if the JComboBox is focus traversable. If the JComboBox is editable
this returns false, otherwise it returns true. |
protected boolean isNavigationKey(int keyCode) {
return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN ||
keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_KP_DOWN;
}
Returns whether or not the supplied keyCode maps to a key that is used for
navigation. This is used for optimizing key input by only passing non-
navigation keys to the type-ahead mechanism. Subclasses should override this
if they change the navigation keys. |
public boolean isPopupVisible(JComboBox c) {
return popup.isVisible();
}
Tells if the popup is visible or not. |
boolean isTableCellEditor() {
return isTableCellEditor;
}
|
static void loadActionMap(LazyActionMap map) {
map.put(new Actions(Actions.HIDE));
map.put(new Actions(Actions.PAGE_DOWN));
map.put(new Actions(Actions.PAGE_UP));
map.put(new Actions(Actions.HOME));
map.put(new Actions(Actions.END));
map.put(new Actions(Actions.DOWN));
map.put(new Actions(Actions.DOWN_2));
map.put(new Actions(Actions.TOGGLE));
map.put(new Actions(Actions.TOGGLE_2));
map.put(new Actions(Actions.UP));
map.put(new Actions(Actions.UP_2));
map.put(new Actions(Actions.ENTER));
}
Populates ComboBox's actions. |
public void paint(Graphics g,
JComponent c) {
hasFocus = comboBox.hasFocus();
if ( !comboBox.isEditable() ) {
Rectangle r = rectangleForCurrentValue();
paintCurrentValueBackground(g,r,hasFocus);
paintCurrentValue(g,r,hasFocus);
}
}
|
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 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);
}
Paints the background of the currently selected item. |
protected Rectangle rectangleForCurrentValue() {
int width = comboBox.getWidth();
int height = comboBox.getHeight();
Insets insets = getInsets();
int buttonSize = height - (insets.top + insets.bottom);
if ( arrowButton != null ) {
buttonSize = arrowButton.getWidth();
}
if(BasicGraphicsUtils.isLeftToRight(comboBox)) {
return new Rectangle(insets.left, insets.top,
width - (insets.left + insets.right + buttonSize),
height - (insets.top + insets.bottom));
}
else {
return new Rectangle(insets.left + buttonSize, insets.top,
width - (insets.left + insets.right + buttonSize),
height - (insets.top + insets.bottom));
}
}
Returns the area that is reserved for drawing the currently selected item. |
public void removeEditor() {
if ( editor != null ) {
unconfigureEditor();
comboBox.remove( editor );
editor = null;
}
}
This public method is implementation specific and should be private.
do not call or override. |
void repaintCurrentValue() {
Rectangle r = rectangleForCurrentValue();
comboBox.repaint(r.x,r.y,r.width,r.height);
}
Repaint the currently selected item. |
protected void selectNextPossibleValue() {
int si;
if ( comboBox.isPopupVisible() ) {
si = listBox.getSelectedIndex();
}
else {
si = comboBox.getSelectedIndex();
}
if ( si < comboBox.getModel().getSize() - 1 ) {
listBox.setSelectedIndex( si + 1 );
listBox.ensureIndexIsVisible( si + 1 );
if ( !isTableCellEditor ) {
comboBox.setSelectedIndex(si+1);
}
comboBox.repaint();
}
}
Selects the next item in the list. It won't change the selection if the
currently selected item is already the last item. |
protected void selectPreviousPossibleValue() {
int si;
if ( comboBox.isPopupVisible() ) {
si = listBox.getSelectedIndex();
}
else {
si = comboBox.getSelectedIndex();
}
if ( si > 0 ) {
listBox.setSelectedIndex( si - 1 );
listBox.ensureIndexIsVisible( si - 1 );
if ( !isTableCellEditor ) {
comboBox.setSelectedIndex(si-1);
}
comboBox.repaint();
}
}
Selects the previous item in the list. It won't change the selection if the
currently selected item is already the first item. |
public void setPopupVisible(JComboBox c,
boolean v) {
if ( v ) {
popup.show();
} else {
popup.hide();
}
}
|
protected void toggleOpenClose() {
setPopupVisible(comboBox, !isPopupVisible(comboBox));
}
Hides the popup if it is showing and shows the popup if it is hidden. |
public void unconfigureArrowButton() {
if ( arrowButton != null ) {
arrowButton.removeMouseListener( popup.getMouseListener() );
arrowButton.removeMouseMotionListener( popup.getMouseMotionListener() );
}
}
This public method is implementation specific and should be private. Do
not call or override. |
protected void unconfigureEditor() {
if (focusListener != null) {
editor.removeFocusListener(focusListener);
}
editor.removeFocusListener(getHandler());
comboBox.getEditor().removeActionListener(getHandler());
}
This protected method is implementation specific and should be private.
Do not call or override. |
protected void uninstallComponents() {
if ( arrowButton != null ) {
unconfigureArrowButton();
}
if ( editor != null ) {
unconfigureEditor();
}
comboBox.removeAll(); // Just to be safe.
arrowButton = null;
}
The aggregate components which compise the combo box are
unregistered and uninitialized. This method is called as part of the
UI uninstallation process. |
protected void uninstallDefaults() {
LookAndFeel.installColorsAndFont( comboBox,
"ComboBox.background",
"ComboBox.foreground",
"ComboBox.font" );
LookAndFeel.uninstallBorder( comboBox );
}
Uninstalls the default colors, default font, default renderer, and default
editor into the JComboBox. |
protected void uninstallKeyboardActions() {
SwingUtilities.replaceUIInputMap(comboBox, JComponent.
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
SwingUtilities.replaceUIActionMap(comboBox, null);
}
Removes the focus InputMap and ActionMap. |
protected void uninstallListeners() {
if ( keyListener != null ) {
comboBox.removeKeyListener( keyListener );
}
if ( itemListener != null) {
comboBox.removeItemListener( itemListener );
}
if ( propertyChangeListener != null ) {
comboBox.removePropertyChangeListener( propertyChangeListener );
}
if ( focusListener != null) {
comboBox.removeFocusListener( focusListener );
}
if ( popupMouseListener != null) {
comboBox.removeMouseListener( popupMouseListener );
}
if ( popupMouseMotionListener != null) {
comboBox.removeMouseMotionListener( popupMouseMotionListener );
}
if (popupKeyListener != null) {
comboBox.removeKeyListener(popupKeyListener);
}
if ( comboBox.getModel() != null ) {
if ( listDataListener != null ) {
comboBox.getModel().removeListDataListener( listDataListener );
}
}
}
Remove the installed listeners from the combo box and its model.
The number and types of listeners removed and in this method should be
the same that was added in installListeners |
public void uninstallUI(JComponent c) {
setPopupVisible( comboBox, false);
popup.uninstallingUI();
uninstallKeyboardActions();
comboBox.setLayout( null );
uninstallComponents();
uninstallListeners();
uninstallDefaults();
if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) {
comboBox.setRenderer( null );
}
ComboBoxEditor comboBoxEditor = comboBox.getEditor();
if (comboBoxEditor instanceof UIResource ) {
if (comboBoxEditor.getEditorComponent().hasFocus()) {
// Leave focus in JComboBox.
comboBox.requestFocusInWindow();
}
comboBox.setEditor( null );
}
if (keySelectionManager instanceof UIResource) {
comboBox.setKeySelectionManager(null);
}
handler = null;
keyListener = null;
focusListener = null;
listDataListener = null;
propertyChangeListener = null;
popup = null;
listBox = null;
comboBox = null;
}
|