Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox setting label and value

Is it possible to set a value and a label to a JComboBox so I can show a label but get a value that is different?

For example in JavaScript I can do:

document.getElementById("myselect").options[0].value //accesses value attribute of 1st option
document.getElementById("myselect").options[0].text //accesses text of 1st option
like image 456
xdevel2000 Avatar asked Apr 14 '11 10:04

xdevel2000


People also ask

What is the difference between JList and JComboBox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

What happens when a user clicks on a JComboBox?

A JComboBox is a compact mechanism for selecting from a small number of options. The options are displayed when the user clicks on the combo box. If it is made editable the user can also enter text that is not supplied as one of the options.

What is JComboBox?

public class JComboBox<E> extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible. A component that combines a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request.


2 Answers

You can put any object inside of a JComboBox. By default, it uses the toString method of the object to display a label navigate in the combo box using the keyboard. So, the best way is probably to define and use appropriate objects inside the combo :

public class ComboItem {
    private String value;
    private String label;

    public ComboItem(String value, String label) {
        this.value = value;
        this.label = label;
    }

    public String getValue() {
        return this.value;
    }

    public String getLabel() {
        return this.label;
    }

    @Override
    public String toString() {
        return label;
    }
}
like image 86
JB Nizet Avatar answered Sep 18 '22 10:09

JB Nizet


Here's a utility interface and class that make it easy to get a combo box to use different labels. Instead of creating a replacement ListCellRenderer (and risking it looking out of place if the look-and-feel is changed), this uses the default ListCellRenderer (whatever that may be), but swapping in your own strings as label text instead of the ones defined by toString() in your value objects.

public interface ToString {
    public String toString(Object object);
}

public final class ToStringListCellRenderer implements ListCellRenderer {
    private final ListCellRenderer originalRenderer;
    private final ToString toString;

    public ToStringListCellRenderer(final ListCellRenderer originalRenderer,
            final ToString toString) {
        this.originalRenderer = originalRenderer;
        this.toString = toString;
    }

    public Component getListCellRendererComponent(final JList list,
            final Object value, final int index, final boolean isSelected,
            final boolean cellHasFocus) {
        return originalRenderer.getListCellRendererComponent(list,
            toString.toString(value), index, isSelected, cellHasFocus);
    }

}

As you can see the ToStringListCellRenderer gets a custom string from the ToString implementation, and then passes it to the original ListCellRenderer instead of passing in the value object itself.

To use this code, do something like the following:

// Create your combo box as normal, passing in the array of values.
final JComboBox combo = new JComboBox(values);
final ToString toString = new ToString() {
    public String toString(final Object object) {
        final YourValue value = (YourValue) object;
        // Of course you'd make your own label text below.
        return "custom label text " + value.toString();
    }
};
combo.setRenderer(new ToStringListCellRenderer(
        combo.getRenderer(), toString)));

As well as using this to make custom labels, if you make a ToString implementation that creates strings based on the system Locale, you can easily internationalize the combo box without having to change anything in your value objects.

like image 25
MB. Avatar answered Sep 21 '22 10:09

MB.