tags:

views:

143

answers:

3

I have a JDialog with a JTextArea component. This dialog shows the user what arguments to use when running the program to have it run the mode they just set up. I have a button on the dialog to copy the arguments to clipboard. This uses the copy() method on the JTextArea object.

This works perfectly and the clipboard contains the correct text up until the program is closed. Then the clipboard is lost. Is there anyway to have it retain this after the program is quit? The normal operation would be to then quit the program and start it again with the arguments.

This might sound strange but the idea is the user would setup an environment with the GUI and then run it with the arguments in a cron or similar.

A: 

I would suggest using the Preferences API for this instead.

TofuBeer
The argument is a path to a configuration file that it should load. It is likely that the user would set up a lot of these so I can't just have a default file that is always loaded at startup.
Android
but you can only have one of them in the clipboard at a time right? so when they change it via your button you save it to the clipboard and then the program uses that value. What I am suggesting is the same thing - press the button and save it via Preferences, then when the program looks for it rather than look in the clipboard it uses the Preferences to grab it. Same data, same flow, jsut look in a different place.
TofuBeer
+1  A: 

I just learned, that there are two clipboards in Java, a local one and a system one. Would explain something, if you just took the local clipboard.

Here's an example that uses the system clipboard. Hope it solves your issue!

Andreas_D
This works but still have the same problem when the program closes.
Android
Very, very strange - can you double-check if you do not accidently copy some empty or other objects to the clipboard just before closing the app, iaw, accidentally overwriting the content?
Andreas_D
I just copy pasted and ran the program on that link. The clipboard is cleared when the program exits. Added a thread.sleep to the last line and the text was on the clipboard up until it ended.
Android
A: 

Works fine for me when I use Ctrl+C to copy the contents of the text component. So try using the supplied Action (which is what Ctrl+C uses) to do the copy instead of the copy() method:

JButton button = new JButton(DefaultEditorKit.CopyAction());
camickr