Java Swing - programmatically copy to clipboard from a JTable

I would like to add a button to my UI which copies the contents of a specific table to the clipboard. I think this should be easy but I can't seem to get it to work or find the solution on the internet. I tried this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ActionEvent nev = new ActionEvent(fileTable, ActionEvent.ACTION_PERFORMED, "copy");
TransferHandler.getCopyAction().actionPerformed(nev);
}

but it has no effect. What's the best way to achieve this? Thanks, Peter

Answers


All Swing components contain Actions that invoked by KeyStrokes. You can reuse this Action.

Action copy = table.getActionMap().get("copy");
ActionEvent ae = new ActionEvent(table, ActionEvent.ACTION_PERFORMED, "");
copy.actionPerformed(ae);

For a list of all Actions check out the Key Bindings.


Need Your Help

alignment requirement for powerpc icbi and dcbf cache instructions

caching memory-alignment powerpc instructions

I have inherited some PowerPC 750FX code. A handful of functions flush the instruction and data cache with

Geektool and tail follow

osx unix cron system tail

I have this LIVE list of formatted urls captured from system log and outputted to a different log file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.