aspose file tools*
The moose likes Java in General and the fly likes Clearing clipboard from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clearing clipboard from java" Watch "Clearing clipboard from java" New topic
Author

Clearing clipboard from java

Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
Hi,

I am running a program from command line.
I need to clear mask the password field.
I have been able to mask it when it is entered via keyboard.But when a user copy pastes I am not able to.
Can you please suggest as to how I can clear the cliepboard or disallow paste.?
This solution should not be OS specific as I need to run the same on unix box as well.

Thanks
Preet


"The more I learn,the more,I get to know, is left to learn."
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
Also this needs to run in Java1.5.
1.6 already has a feature for that but cannot use it.

Preet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Check out java.awt.datatransfer.Clipboard, in combination with java.awt.datatransfer.StringSelection. Create a StringSelection with an empty String as selection to set as the clipboard contents to clear it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
Thanks a lot.
Was able to use this and solve my problem. Got the control over system clipboard and this siolved my issue.

Also just as an information, I tried this on unix and this failed.
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at sun.awt.HeadlessToolkit.getSystemClipboard(HeadlessToolkit.java:306)
at TextTransfer.getClipboardContents(TextTransfer.java:36)
at TextTransfer.main(TextTransfer.java:16)


export DISPLAY=:0.0 solved my problem.
Then I got to know that any awt component running on Unix needs an Xserver which was not present.
Have started installing it.
But thanks a lot for all the help Rob.
Thanks a ton.

Regards
Preet
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
Thanks a lot for all the help.
Its working perfectly fine after xserver was installed on UNIX.
Thanks once again.

Regards
Preet
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

You could also try to run your Java program with the "-Djava.awt.headless=true" flag. This flag should come before the class name, and it allows you to use many (but not all; any visible GUI is still not going to work) features from AWT and Swing that would otherwise throw a HeadlessException.
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
Hi,

The clipboard problem has been solved.
But now I have another issue.
I am trying to mask the characters entered by keyboard.
I am using a PushBackStream to do this by using "unread()" to erase the entered characters. This being done inside a thread.
But if I type my characters fast, some of the characters are missed out by this function and they can be seen.

And since its 1.5,cannot use the Console class that is available in 1.6.

Please help with this.
One thing that I could think of is a way to disable the printing of what we write(something like disabling echo in Unix).But this should be doable on all of the OS.

Thanks in advance,

Regards
Preet
 
 
subject: Clearing clipboard from java