• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clearing clipboard from java

 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also this needs to run in Java1.5.
1.6 already has a feature for that but cannot use it.

Preet
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic