我的要求是,如果用户按Ctrl+ C。组合框中的文本将自动复制(不突出显示,将复制整个单词)。所以,如果我去另一个组合框,我可以粘贴它。我是java新手,不知道怎么做,
这是场景要求的说明:
到目前为止我只有这个
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);
发布于 2018-10-10 11:23:42
为了其他可能会遇到这种奇怪需求的人的利益
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);
https://stackoverflow.com/questions/-100008896
复制相似问题