Java – Copy the text Java in the combo box

Copy the text Java in the combo box… here is a solution to the problem.

Copy the text Java in the combo box

My requirement is that if the user presses Ctrl + C. The text in the combo box will be copied automatically (no highlighting, the whole word will be copied). So if I go to another combo box, I can paste it. I’m new to Java and don’t know how

This is a description of the scenario requirements:

  1. The cursor is in the first combo box, indicating that this is the current one
    Editable.

    enter image description here

  2. The user presses Ctrl + C

  3. The user selects the third combo box

  4. (the cursor in the third combo box indicates that this is current.)
    Editable) and press Ctrl + V
    enter image description here

At the moment I only have this

JTextComponent editor = (JTextComponent) combo.getEditor().getEditorComponent();
    Action test = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            do function here
        }
    };

editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "test");
    editor.getActionMap().put("test", test);

Solution

I did something similar :

I used JComboBox’s own InputMap and ActionMap, and used WHEN_ANCESTOR_OF_FOCUSED_COMPONENT’s InputMap conditional.

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class ComboCopy extends JPanel {
    private static final int COMBO_COUNT = 3;
    private static final String[] DATA = {"Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday"};
    private List<JComboBox<String>> combos = new ArrayList<>();

public ComboCopy() {
        for (int i = 0; i < COMBO_COUNT; i++) {
            JComboBox<String> combo = new JComboBox<>(DATA);
            setBindings(combo);
            combos.add(combo);
            add(combo);
        }
    }

private void setBindings(final JComboBox<String> combo) {
        int condition = WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
        InputMap inputMap = combo.getInputMap(condition);
        ActionMap actionMap = combo.getActionMap();

KeyStroke ctrlC = KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK);
        KeyStroke ctrlV = KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_DOWN_MASK);

inputMap.put(ctrlC, ctrlC.toString());
        actionMap.put(ctrlC.toString(), new AbstractAction() {

@Override
            public void actionPerformed(ActionEvent e) {
                String text = (String) combo.getSelectedItem();
                if (text != null) {
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    Transferable contents = new StringSelection(text);
                    clipboard.setContents(contents, null);
                }
            }
        });
        inputMap.put(ctrlV, ctrlV.toString());
        actionMap.put(ctrlV.toString(), new AbstractAction() {

@Override
            public void actionPerformed(ActionEvent e) {
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable contents = clipboard.getContents(null);
                if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text;
                    try {
                        text = (String) contents.getTransferData(DataFlavor.stringFlavor);
                        combo.setSelectedItem(text);
                    } catch (UnsupportedFlavorException | IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });

}

private static void createAndShowGui() {
        ComboCopy mainPanel = new ComboCopy();

JFrame frame = new JFrame("ComboCopy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Related Problems and Solutions