gpt4 book ai didi

java - 使用剪贴板时 KeyListener 中的奇怪行为

转载 作者:行者123 更新时间:2023-11-29 03:38:49 25 4
gpt4 key购买 nike

我试图为我的 JTable 做一个简单的 ctrl+c,但是当使用控制按钮时,整行被复制,而不是指定的单元格。如何获得预期的效果?

public class ClipboardTable {

private static final Clipboard clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
public static JTable tab;

public static void copyToClipboard(JTable ctaable) {
StringBuilder str = new StringBuilder("");
for (int i = 0; i < ctaable.getSelectedRows().length; i++) {
str.append(ctaable.getValueAt(ctaable.getSelectedRows()[i], 0));
}
StringSelection strsel = new StringSelection(str.toString());
clipboard.setContents(strsel, strsel);
System.out.println("expected: "+str.toString());
}

public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object[][] data = {{1,2,3},{5,6,7},{8,9,0}};
tab = new JTable(data, new String[] {"a","b","c"});
KeyListener searchKeyListen = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
ClipboardTable.copyToClipboard(ClipboardTable.tab); //this works fine
if (e.isControlDown()) {
if ((e.getKeyCode() == KeyEvent.VK_C)) {
ClipboardTable.copyToClipboard(ClipboardTable.tab); // whole row is copied instead of only Cell
}
}
}
};
tab.addKeyListener(searchKeyListen);
jf.add(tab);
jf.pack();
jf.setVisible(true);
}
}

最佳答案

使用Keybindings对于 Swing,因为 KeyListener/KeyAdapter 在与 Swing 组件一起使用时存在已知问题。

上面的实现似乎工作正常:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.KeyStroke;

public class ClipboardTable {

private static final Clipboard clipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
public static JTable tab;

public static void copyToClipboard(JTable ctaable) {
StringBuilder str = new StringBuilder("");
for (int i = 0; i < ctaable.getSelectedRows().length; i++) {
str.append(ctaable.getValueAt(ctaable.getSelectedRows()[i], 0));
}
StringSelection strsel = new StringSelection(str.toString());
clipboard.setContents(strsel, strsel);
System.out.println("expected: " + str.toString());
}

public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object[][] data = {{1, 2, 3}, {5, 6, 7}, {8, 9, 0}};
tab = new JTable(data, new String[]{"a", "b", "c"});

tab.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK, true), "CTRL C");
tab.getActionMap().put("CTRL C", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
ClipboardTable.copyToClipboard(ClipboardTable.tab); // whole row is copied instead of only Cell
}
});

jf.add(tab);
jf.pack();
jf.setVisible(true);
}
}

关于java - 使用剪贴板时 KeyListener 中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14126668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com