如何将jtable的内容复制到剪贴板
我已将数据归档到jtable。我想为 Jbutton 的操作创建 java 代码。我的要求是当我单击按钮时,然后复制 jtable 的所有内容到剪贴板。我怎样才能做到这一点。
String[] columnNames={"DATE","Steet"};
String[][] cells=new String[ar.size()][2];
for(int i=0;i<ar.size();i++){
cells[i][0]=((PRIvariable)ar.get(i)).incDate;
cells[i][1]=((PRIvariable)ar.get(i)).selectedSteer;
}
table = new JTable(cells,columnNames);
table.setVisible(true);
table.setSize(400, 400);
js=new JScrollPane();
js.setViewportView(table);
js.setBounds(10, 230,500, 215);
js.setVisible(true);
add(js,java.awt.BorderLayout.CENTER);
- 在此代码中 ar 是我的数组列表。
- 我怎样才能编写可以复制这个Jtable内容的代码。
i have jtable filed with data. i want to create java code for Action of the Jbutton. My requirement is when i click the button, then copy all the content of jtable to clip board. how can i do that.
String[] columnNames={"DATE","Steet"};
String[][] cells=new String[ar.size()][2];
for(int i=0;i<ar.size();i++){
cells[i][0]=((PRIvariable)ar.get(i)).incDate;
cells[i][1]=((PRIvariable)ar.get(i)).selectedSteer;
}
table = new JTable(cells,columnNames);
table.setVisible(true);
table.setSize(400, 400);
js=new JScrollPane();
js.setViewportView(table);
js.setBounds(10, 230,500, 215);
js.setVisible(true);
add(js,java.awt.BorderLayout.CENTER);
- in this code ar is my arraylist.
- how can i write the code witch can copy the content of this Jtable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JTable 已经支持复制操作。 操作地图操作展示了如何轻松地使用此操作JButton,因此您不必重写代码。
JTable already supports a copy Action. Action Map Action shows how you can easily use this Action with a JButton so you don't have to rewrite the code.
当我过去需要这样做时,我从这里的代码开始:
http://www.javaworld.com/javatips/jw-javatip77.html
并进行修改,为按钮创建一个操作,将数据和列标题从表复制到剪贴板。
When I needed to do this in the past I started with the code here:
http://www.javaworld.com/javatips/jw-javatip77.html
And modified to create an action for a button that would copy the data and the column headings from a table to the clipboard.
这是来自 javaworld 的 ExcelAdapter 的更新版本(从 1999 年开始)。 链接
使用:
<强>ClipboardKeyAdapter.java
Here is a renewed version of ExcelAdapter from javaworld (from 1999). Link
For use:
ClipboardKeyAdapter.java
Gere的答案效果很好,但我发现将事件从 keyReleased 更改为 keyPressed 更灵敏
根据
我的经验,如果我非常快地释放到 V 键,并且按下该键时需要按住更长时间,则 keyReleased 不会总是触发瞬间发射。
Gere's answer works well but I found it more responsive to change the event from keyReleased to keyPressed
to
In my experience the keyReleased wouldn't always fire if I released to V key very quickly and it needed to be held down for longer whereas the key pressed fired instantaneous.