How can I flip a jComboBox
so that the popup menu button is on the left rather than on the right?
I have tried:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but here is the result:
Join Stack Overflow to learn, share knowledge, and build your career.
How can I flip a jComboBox
so that the popup menu button is on the left rather than on the right?
I have tried:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but here is the result:
The location of the drop-down arrow is controlled by the ComboBoxUI
associated with the JComboBox
. Generally, if you want to change this behavior, you have to create your own implementation of ComboBoxUI
. Fortunately, for your specific need there is another way. The default ComboBoxUI
is coded to place the arrow on the right by default, but it will place the arrow on the left if the component orientation is changed to right-to-left:
JComboBox<String> comboBox = new JComboBox<>(new String[]{"One", "Two", "Three"});
comboBox.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
As you can see, this will affect the orientation of the component as a whole, but it does not affect the orientation of the list box items within the combo box. To make this adjustment, call applyComponentOrientation
on the ListCellRenderer
associated with the component. If you have a custom renderer, you can just call the method on that object. With the default renderer, it's a little trickier but still possible:
ListCellRenderer<? super String> defaultRenderer = comboBox.getRenderer();
ListCellRenderer<String> modifiedRenderer = new ListCellRenderer<String>() {
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index,
boolean isSelected, boolean cellHasFocus) {
Component component = defaultRenderer.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
component.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
return component;
}
};
comboBox.setRenderer(modifiedRenderer);
Finally, if your combo box is editable, you'll likely need to use applyComponentOrientation
on the editor component as well.
applyComponentOrientation
on the JComboBox
instead of setComponentOrientation
, it might work, but I can't test it
– Kevin K
Dec 3 '17 at 5:02