gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:22:38 25 4
gpt4 key购买 nike

我遇到一个问题,我试图复制 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

我在这里做错了什么吗?

最佳答案

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

关于java - 如何将 JList 元素文本复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224259/

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