我的要求是,如果用户按 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);
答案 0 :(得分:2)
我做了类似的事情:
我使用了JComboBox本身的InputMap和ActionMap,并使用了WHEN_ANCESTOR_OF_FOCUSED_COMPONENT的InputMap条件。
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());
}
}
答案 1 :(得分:1)
为了可能遇到这个奇怪要求的其他人的利益。
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);