Making password fields in GenericDialogs

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Making password fields in GenericDialogs

Jeff Brandenburg
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)
Reply | Threaded
Open this post in threaded view
|

Re: Making password fields in GenericDialogs

dscho
Hi,

On Fri, 22 Jul 2005, Jeff Brandenburg wrote:

> 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('*');
>      }
>    }

How about something like this instead:

public void addPasswordField(String label, String default) {
        addStringField(label,default);
        TextField tf = (TextField)stringField.elementAt(stringField.size()-1);
        tf.setEchoChar('*');
}

BTW I would vote against changing GenericDialog: It is just not meant to
handle dialogs independent from image processing. Besides, I don't think
it is a kludge to extend GenericDialog to your needs by inheriting it.

Ciao,
Dscho