PDA

View Full Version : Copying to clipboard in java



skipsbro
November 18th, 2008, 01:20 AM
How can I copy a string in lava to the clipboard?

tinny
November 18th, 2008, 07:05 AM
How can I copy a string in lava to the clipboard?

Have a look at the JDIC (Java Desktop Integration Components) project.

https://jdic.dev.java.net/

I believe they have some sort of API for this.

cl333r
November 18th, 2008, 12:15 PM
If you don't wanna dig into the APIs:
JTextFied tf = new JTextField();
tf.setText( yourString );
tf.copy();

drubin
November 18th, 2008, 05:11 PM
If you don't wanna dig into the APIs:
JTextFied tf = new JTextField();
tf.setText( yourString );
tf.copy();

If that is in the standard java libs. Then you can definitely take a look at the JTextField/parents components and see how they do it and just implmeent that.

Quick google.
http://www.javapractices.com/topic/TopicAction.do?Id=82
http://www.devx.com/Java/Article/22326

tinny
November 18th, 2008, 08:31 PM
If you don't wanna dig into the APIs:
JTextFied tf = new JTextField();
tf.setText( yourString );
tf.copy();

Cool, that looks like what the OP wants.

Quote from the javadoc


Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. The current selection remains intact. Does nothing for null selections.

drubin
November 18th, 2008, 08:36 PM
Cool, that looks like what the OP wants.

Quote from the javadoc


Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. The current selection remains intact. Does nothing for null selections.

According to that quote you are going to have to select the text as well.
Start (http://www.docjar.com/docs/api/javax/swing/text/JTextComponent.html#setSelectionStart(int))
End (http://www.docjar.com/docs/api/javax/swing/text/JTextComponent.html#setSelectionEnd(int))


If you don't wanna dig into the APIs:
JTextFied tf = new JTextField();
tf.setText( yourString );
tf.copy();

Question: will this work with out the Component being displayed on the screen?