Add this line to your code,

jop.setInitialSelectionValue(password);

If you use a custom UI, you can replace the JTextField instance with a
JPasswordField instance by overriding the getMessage() method,

import javax.swing.plaf.basic.BasicOptionPaneUI;
...

class MyOptionPaneUI extends BasicOptionPaneUI {
         class TextFieldActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                optionPane.setInputValue(
                    ((JTextField)e.getSource()).getText());
            }
         }

         protected Object getMessage() {
            Object sv = super.getMessage();
            if (sv instanceof Object[] && 
                ((Object[])sv)[1] instanceof JTextField) {
                JPasswordField pf = new JPasswordField();
                pf.addActionListener(new TextFieldActionListener());
                Object inputValue = optionPane.getInitialSelectionValue();
                if (inputValue != null) {
                    String inputString = inputValue.toString();
                    pf.setText(inputString);
                }
                ((Object[])sv)[1] = pf;
                inputComponent = pf;
            }
            return sv;
         }
}


JOptionPane jop = new ...
jop.setUI(new MyOptionPaneUI());
...



----- Original Message -----
From: Bill Tschumy <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 09, 2001 4:17 AM
Subject: Problem parameterizing JOptionPane


> I am trying to use JOptionPane to allow a user to enter some String
> data.  Things are going well but I can't initialize the input
> textfield with a value.  This is for part of a Preference panel, so I
> want to initialize it with the old preference string.  The problem is
> that despite using JOptionPane's setInputValue(), the dialog's
> textfield initially comes up empty.  Am I going about this wrong?
> The code  is below.
>
>      private void setAutoSendPassword()
>      {
>          String message = "Enter password for auto-send:";
>          String password = autoSendPassword;
>          JOptionPane jop = new JOptionPane(message,
>                                            JOptionPane.QUESTION_MESSAGE,
>                                            JOptionPane.OK_CANCEL_OPTION);
>          jop.setWantsInput(true);
>          jop.setInputValue(password);
>          JDialog dialog = jop.createDialog(this, "Enter Password");
>          dialog.setResizable(false);
>          dialog.toFront();
>          dialog.setVisible(true);
>          Object selectedValue = jop.getValue();
>          if (selectedValue != null)
>          {
>              password = (String)jop.getInputValue();
>              autoSendPassword = password;
>          }
>      }
>
> I am running this on Mac OS X (using JRE 1.3)
>
>
> Also, as you can see, I am using this to enter a password.  Is there
> a way to get JOptionPane to use a JPasswordField rather than a
> JTextField???
>
> Thanks.
> --
> Bill Tschumy
> Otherwise -- Austin, TX
> [EMAIL PROTECTED]
> _______________________________________________
> Advanced-swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/advanced-swing








_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to