Java Utililty Methods JComboBox Event

List of utility methods to do JComboBox Event

Description

The list of methods to do JComboBox Event are organized into topic(s).

Method

voidaddActionListener(ActionListener listener, JComboBox... components)
add Action Listener
for (JComboBox component : components) {
    component.addActionListener(listener);
voidaddActionListeners(JComboBox comboBox, ActionListener[] listeners)
add Action Listeners
for (ActionListener listener : listeners) {
    comboBox.addActionListener(listener);
JComboBoxcreateComboBox(String actionCommand, ActionListener listener, String... items)
create Combo Box
JComboBox box = new JComboBox();
box.setEditable(false);
for (String item : items) {
    box.addItem(item);
if (listener != null) {
    box.addActionListener(listener);
    box.setActionCommand(actionCommand);
...
JComboBoxcreateComboBox(TYPE[] items, ActionListener... listeners)
create Combo Box
JComboBox<TYPE> result = new JComboBox<>(items);
for (ActionListener listener : listeners) {
    result.addActionListener(listener);
return result;
voidinstallComboBoxCopyAction(JComboBox comboBox)
install Combo Box Copy Action
final Component editorComponent = comboBox.getEditor().getEditorComponent();
if (!(editorComponent instanceof JTextComponent))
    return;
final InputMap inputMap = ((JTextComponent) editorComponent).getInputMap();
for (KeyStroke keyStroke : inputMap.allKeys()) {
    if (DefaultEditorKit.copyAction.equals(inputMap.get(keyStroke))) {
        comboBox.getInputMap().put(keyStroke, DefaultEditorKit.copyAction);
comboBox.getActionMap().put(DefaultEditorKit.copyAction, new AbstractAction() {
    @Override
    public void actionPerformed(final ActionEvent e) {
        if (!(e.getSource() instanceof JComboBox))
            return;
        final JComboBox comboBox = (JComboBox) e.getSource();
        final String text;
        final Object selectedItem = comboBox.getSelectedItem();
        if (selectedItem instanceof String) {
            text = (String) selectedItem;
        } else {
            final Component component = comboBox.getRenderer().getListCellRendererComponent(new JList(),
                    selectedItem, 0, false, false);
            if (component instanceof JLabel) {
                text = ((JLabel) component).getText();
            } else if (component != null) {
                final String str = component.toString();
                text = str == null || str.startsWith(component.getClass().getName() + "[") ? null : str;
            } else {
                text = null;
        if (text != null) {
            final JTextField textField = new JTextField(text);
            textField.selectAll();
            textField.copy();
});
voidsetAction(JComboBox cmb, ActionListener listener, String actionCommand)
set Action
cmb.addActionListener(listener);
cmb.setActionCommand(actionCommand);