Java JComboBox Event installComboBoxCopyAction(JComboBox comboBox)

Here you can find the source of installComboBoxCopyAction(JComboBox comboBox)

Description

install Combo Box Copy Action

License

Apache License

Declaration

public static void installComboBoxCopyAction(JComboBox comboBox) 

Method Source Code


//package com.java2s;
/*//from w ww. java  2s .  c  om
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;

import javax.swing.text.DefaultEditorKit;

import javax.swing.text.JTextComponent;

import java.awt.*;
import java.awt.event.*;

public class Main {
    public static void installComboBoxCopyAction(JComboBox comboBox) {
        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();
                        // skip default Component.toString and handle SimpleColoredComponent case
                        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();
                }
            }
        });
    }
}

Related

  1. addActionListener(ActionListener listener, JComboBox... components)
  2. addActionListeners(JComboBox comboBox, ActionListener[] listeners)
  3. createComboBox(String actionCommand, ActionListener listener, String... items)
  4. createComboBox(TYPE[] items, ActionListener... listeners)
  5. setAction(JComboBox cmb, ActionListener listener, String actionCommand)