0

Hello I am using a JPasswordField when I want to read it it is no problem with getPassword but what I am doing is when the Password is not set it shows a InputDialog where you can type in the password and then it should set the the Password in to the JPasswordField but when I use setText it does not set it and there is not method setPassword(). So my question is how can i set a password to a JPasswordField?

String password = "";
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
Object[] obj = {"Bitte ihr PAsswort eingeben:\n\n", passwordField};
Object stringArray[] = {"OK","Cancel"};

if (JOptionPane.showOptionDialog(null, obj, "Passwort", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.WARNING_MESSAGE)
{
password = new String(passwordField.getPassword());
}

txtFtpUser.setText(username);
panel_1.remove(txtFtpPassword);
txtFtpPassword = new JPasswordField(password);
txtFtpPassword.setBounds(10, 113, 206, 23);
panel_1.add(txtFtpPassword);
The Cat
  • 2,375
  • 6
  • 25
  • 37
alexj
  • 139
  • 1
  • 4
  • 15
  • http://docs.oracle.com/javase/7/docs/api/javax/swing/JPasswordField.html#JPasswordField%28java.lang.String%29 – KrHubert Nov 09 '12 at 10:55
  • Whithout some code snippet it's hard to tell you what's wrong. – Mickäel A. Nov 09 '12 at 11:02
  • I did txtPassword = new JPasswordField("PASSWORD"); but it still didnt work. I also tried panel.remove(txtPassword); defined it new, setBounds and added it again with panel.add(txtPassword) and it also didnt work. – alexj Nov 09 '12 at 11:02
  • Can't you do something like [this][1] [1]: http://stackoverflow.com/a/9108316/37298 – Shervin Asgari Nov 09 '12 at 11:07
  • Is the layout of "panel_1" null ? And why do I see 2 JPasswordField (ie "passwordField" and "txtFtpPassword"). Are your sure you used the right one ? – Mickäel A. Nov 09 '12 at 11:07
  • The upper Passwordfield is for the Input at the Dialog the lower one is the one that is on the Form. No panel_1 has a absolute layout and is tabbed Panel – alexj Nov 09 '12 at 11:11
  • @Shervin no setText does not work on a JPasswordField – alexj Nov 09 '12 at 11:11
  • I dont understand why you want to use 2 password fields. Are you sure that does make sense? If so, cant you refer the second password field to the first one so that you only use one object? – Nils Nov 09 '12 at 11:14
  • The first PasswordField is the Field that will be added to the Dialog the second one is the one that is on the GUI. I do it like that because it looks better for me. – alexj Nov 09 '12 at 11:18
  • Can polymorphism cause JPasswordField to work incorrectly? –  Oct 15 '15 at 01:00

2 Answers2

4

You claim that setText is not working for a JPasswordField is incorrect. See the following piece of code which just works as expected:

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame testFrame = new JFrame( "Test" );
        JPasswordField field = new JPasswordField(  );
        field.setColumns( 20 );
        field.setText( "Password" );
        testFrame.add( field );
        testFrame.pack();
        testFrame.setVisible( true );
        testFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
      }
    } );
  }

The variant where you pass the text in the constructor (as you did in your code) also works as expected.

So I would search in another direction. The following part

txtFtpUser.setText(username);
panel_1.remove(txtFtpPassword);
txtFtpPassword = new JPasswordField(password);
txtFtpPassword.setBounds(10, 113, 206, 23);
panel_1.add(txtFtpPassword);

makes me wonder whether you see your new JPasswordField in your UI. When you add/remove components from a Container you need to invalidate the layout, as documented in the Container#add and Container#remove methods.

Note: be aware of the security issues when passing the password around as a String. But according to your comments you are already aware of this.

Robin
  • 36,233
  • 5
  • 47
  • 99
  • 1
    @alexj that is not shown in your code. Consider posting an [SSCCE](http://sscce.org) so we can stop guessing and provide some real answers – Robin Nov 09 '12 at 11:38
  • When i remove it it is removed when I add it it doesent have anything inside – alexj Nov 09 '12 at 11:41
  • @alexj: You may also want to minimize shoulder-surfing, as shown [here](http://stackoverflow.com/a/5342146/230513). – trashgod Nov 09 '12 at 13:45
1

I found the Error here it was:

if (JOptionPane.showOptionDialog(null, obj, "Passwort", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.WARNING_MESSAGE)
{
password = new String(passwordField.getPassword());
}

It checked it was a Warning Message but it was a YES_OPTION :) Thank you for all your help.

alexj
  • 139
  • 1
  • 4
  • 15