Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes special chars generated during paste/copy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "special chars generated during paste/copy" Watch "special chars generated during paste/copy" New topic

special chars generated during paste/copy

Jack Wanes

Joined: Sep 18, 2006
Posts: 22
I am trying to get paste/copy events in a JTextField when user uses CTRL-V or CTRL-C to paste or copy texts.

The strange thing is, every time I use ctrl-v, ctrl-c, or ctrl-x to paste/copy/cut, there is always a special character (like a small square, or rectangle?) showing up at the end.

I have to use jdk1.4.1 since I am working on a old product. That does not happen in jdk1.5 environment.

I used getText() to get pasted text and then did a trim like this:

String pastedText = getText().trim();

But it seems the length of a pasted text is still the same after trim.
Is this a jdk1.4 bug?
How can I get rid of this unwanted special character?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Don't know. These blocks are annoying; they usually appear on shells or command prompt windows, and represent characters which are outwith the capabilities of the OS to print. Since Swing supports the whole of Unicode it oughtn't to print blocks.
Suggest as a first step:-
  • Add a print button to your GUI,
  • Add an ActionListener to it,
  • In that ActionListener call a method which scans the text in the TextField,
  • In that method, break the text into chars (toCharArray()),
  • Print the individual characters, but cast to an int first. See if you can print them in hex.
  • Look up their Unicode values here

  • Another way to do it is to try a substring missing out the last character in the String.
    I tried programming an app to mimic your problem and ran it with J1.4.2:-This does not have any boxes or squares in. So it probably isn't a bug in J1.4
    Jack Wanes

    Joined: Sep 18, 2006
    Posts: 22
    Thanks for the post. I guess its not a 1.4.1 bug since I cannot duplicate in a standalone application. But somehow the legacy app just produces this control character.
    I agree. Here's the link:
    subject: special chars generated during paste/copy
    It's not a secret anymore!