如何将 JList 元素文本复制到剪贴板

How to copy JList element text to clipboard

我在尝试复制 JList 元素的内容(元素中显示的文本)然后将该内容粘贴到另一个 JList 元素时遇到问题。问题是,出于某种原因,无论我在实际粘贴文本时制作的实际剪贴板内容是什么,它都会粘贴我假设的元素的 toString() 方法。看这里:

package listtest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class main extends JPanel {
    JList<Test> list;

    DefaultListModel<Test> model;

    int counter = 15;

    public main() {
    setLayout(new BorderLayout());
    model = new DefaultListModel<Test>();
    list = new JList<Test>(model);
    Test[] elements = new Test[] {new Test("Test 1"), new Test("Test 2"), new Test("Test 3")};
    for (int i = 0; i < elements.length; i++) {
        model.addElement(elements[i]);
    }

    add(list, BorderLayout.NORTH);
    list.setCellRenderer((ListCellRenderer<? super Test>) getRenderer());

    list.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_V && e.isControlDown()) {
            Clipboard c = getSystemClipboard();
            try {
            String clip = (String) c.getData(DataFlavor.stringFlavor);
            System.out.println("Clipboard contents when pasting: " + clip);
            } catch (UnsupportedFlavorException | IOException e1) {   }     
        }else if(e.getKeyCode() == KeyEvent.VK_C && e.isControlDown()) {
            Clipboard c = getSystemClipboard();
            StringSelection a = new StringSelection("Hello");
            try {
            System.out.println("StringSelection I just made: " + a.getTransferData(DataFlavor.stringFlavor));
            c.setContents(a, null);
            String clip = (String) c.getData(DataFlavor.stringFlavor);
            System.out.println("Clipboard contents right after copying: " + clip);
            } catch (UnsupportedFlavorException | IOException e1) {}
        }
        }
    });

    }

    private ListCellRenderer<? super String> getRenderer() {
    return new DefaultListCellRenderer(){
        private static final long serialVersionUID = 17990901717809235L;
        @Override
        public Component getListCellRendererComponent(JList<?> list,
            Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus);
        listCellRendererComponent.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.BLACK));
        setText(((Test)value).testStr);
        return listCellRendererComponent;
        }
    };
    }

    private Clipboard getSystemClipboard(){
    Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
    Clipboard systemClipboard = defaultToolkit.getSystemClipboard();

    return systemClipboard;
    }

    public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new main());
    frame.setSize(260, 200);
    frame.setVisible(true);
    }
}

class Test{

    public String testStr;

    public Test(String test) {
    testStr = test;
    }
}

在这个程序中,只要你在选中一个元素的情况下按 ctrl+C,它就会将 "Hello" 添加到剪贴板,然后检查剪贴板并将其打印出来。当您按下 ctrl+V(实际上是在任何地方。您也可以在记事本中尝试)时,它会粘贴您按下 ctrl+c 的元素的对象名称,而不是实际的剪贴板内容(在本例中为 "Hello")。简而言之,输出是:

StringSelection I just made: Hello
Clipboard contents right after copying: Hello
Clipboard contents when pasting: listtest.Test@12ffd81

我是不是做错了什么?

看起来 windows 当您在选择一个元素时按 ctrl+c 时仍然会尝试复制,并且它显然会获得所选内容的 toString() 表示形式。我将复制代码移至 keyReleased() 而不是 keyPressed(),现在它按预期工作了