The Source for Java Technology Collaboration
User: Password:



Scott Violet

Scott Violet's Blog

JPasswordField with an empty echo character: the fix

Posted by zixle on December 19, 2005 at 03:38 PM | Comments (5)

In my last blog I explained how to customize the visual feedback provided by JPasswordField. Unfortunately the first option I detailed, specifying a character that takes no space, had a bug in it. This blog discusses the bug and how to fix it. If you want to cut to the chase and run the demo it can be found here.

Specifying you want JPasswordField to render a character with no space is as easy as:

passwordField.setEchoCharacter('\u200b');
As my last demo illustrated, this will produce an ArithmeticException when you click on the password field (second one in the demo). As you could probably guess the exception occurs because PasswordView is dividing the width of the character without checking it, resulting in the exception. The fix is to create a subclass of PasswordView that overrides viewToModel without dividing by the character width. Here's the code:
  public int viewToModel(float fx, float fy, Shape boundsAsShape, Position.Bias[] bias) {
    boundsAsShape = adjustAllocation(boundsAsShape);
    Rectangle bounds = (boundsAsShape instanceof Rectangle) ?
                (Rectangle)boundsAsShape : boundsAsShape.getBounds();
    if (fx < bounds.x) {
      return getStartOffset();
    }
    return getEndOffset();
  }
With a character that takes up no space there's no way for the user to click anywhere but the beginning or end of the text. As such, viewToModel will return one of these.

To install this View you need to do something similar to that of the last blog (refer to it for more details). Create a custom JPasswordField, with a custom UI that returns the new View:

  new JPasswordField() {
    public void updateUI() {
      setUI(new BasicPasswordFieldUI() {
        public View create(Element elem) {
          return new EmptySpacePasswordView(elem);
        }
      });
    }
  };
And here's the demo.

That's enough for passwordfield and text, next time around something new.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • So is this something that is impossible to do in a look-and-feel independent manner? Or just a lot more work?

    Posted by: jayds on December 20, 2005 at 10:04 AM

  • jayds,

    You could provide subclasses for the known UIs, Windows, GTK, Metal, Motif... But it's certainly painful and you can obviously only do it for those you know about.

       -Scott

    Posted by: zixle on December 20, 2005 at 10:16 AM

  • Good article! Thanks for sharing! 搬屋 月子中心 搬屋服務

    Posted by: winrelocation on August 04, 2007 at 11:45 PM

  • Or just a lot more work?

    租车公司
    月餅
    租車

    Posted by: peak on August 08, 2007 at 12:26 AM

  • 1,月饼
    2,月饼
    3,月饼
    4,月饼
    5,月饼
    6,月饼

    Posted by: mooncake on August 20, 2007 at 04:29 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds