1

我尝试为我的 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);
}
}
4

3 回答 3

2

使用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);
    }
}
于 2013-01-02T18:03:08.670 回答
1

您可以设置每个单元格的选择范围:

tab.setCellSelectionEnabled(true);

请注意,Swing 中的首选方法是使用Key BindingsKeyListeners

于 2013-01-02T17:54:53.410 回答
1

你应该

  • 去除那个KeyListener
  • 而是选择Action具有正确键绑定的 。因为Action你可以使用TransferHandler.getCopyAction()
  • 如果您对标准行为不满意,请自定义TransferHandler您的JTable
于 2013-01-02T17:56:25.693 回答