Login  Register

Making password fields in GenericDialogs

Posted by Jeff Brandenburg on Jul 22, 2005; 5:21pm
URL: http://imagej.273.s1.nabble.com/Making-password-fields-in-GenericDialogs-tp3705203.html

Hi, Wayne, everybody --

I'm working on a plugin that loads and displays images from a Tomcat/JSP
Web server.  Users will sometimes leave ImageJ idle long enough for a
session to expire, and I need to re-authenticate, which entails
prompting for a username and password.  I'd like to take advantage of
the GenericDialog facility, but that doesn't offer an addPasswordField()
method.

As a workaround, I've made a subclass of GenericDialog, adding the
following method:

   public void makePasswordField() {
     if (stringField.size() > 0) {
       TextField tf = (TextField)
this.stringField.elementAt(this.stringField.size()-1);
       tf.setEchoChar('*');
     }
   }

Calling this method sets the echoChar for the last stringField added to
the dialog.  (Fortunately, stringField is protected, not private, so I
can get away with this.)

I can use this method as follows:

   ReauthorizationDialog d = new ReauthorizationDialog("Hey!");
   d.addMessage("Your session has expired.");
   d.addStringField("Username:", "");
   d.addStringField("Password:", "");
   d.makePasswordField();

I could, of course, add further fields after this -- makePasswordField()
only affects the last string field added before it's called.

This works, although it's pretty much of a kludge.  Wayne, would it be
worthwhile adding this to GenericDialog, or adding a variant of the
addStringField() method that accepts an echo character?
--
        -jeffB (Jeff Brandenburg, Duke Center for In-Vivo Microscopy)