Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy text in combo-box Java

My requirement is that if user press Ctrl + C. The text in the combo-box will automatically be copied (without it being highlighted, the whole word would be copied). So if I go to another combo-box, I could paste it. I am new to java and don't have an idea how to it,

This is an illustration of the scenario requirement:

  1. Cursor is at the first combo-box indicating that this is the current editable.

    enter image description here

  2. User press Ctrl + C

  3. User selects third combo-box (Cursor is at the third combo-box indicating that this is the current editable) then press Ctrl + V enter image description here

So far 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);
like image 858
gengencera Avatar asked Sep 25 '26 09:09

gengencera


2 Answers

I did something similar:

I used the InputMap and ActionMap of the JComboBox itself, and used the InputMap condition of WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.

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());
    }
}
like image 53
Hovercraft Full Of Eels Avatar answered Sep 27 '26 22:09

Hovercraft Full Of Eels


For the benefit of other people who might encounter this weird requirement.

JTextComponent editor = (JTextComponent) combo.getEditor().getEditorComponent();

    Action copyAllText = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            String item = combo.getEditor().getItem().toString().trim();
            StringSelection stringSelection = new StringSelection(item);
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, null);
        }
    };

    editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "copyAllText");
    editor.getActionMap().put("copyAllText", copyAllText);
like image 22
gengencera Avatar answered Sep 27 '26 22:09

gengencera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!