java - Can not get text from PasswordField even converted to String -
i cant not input jpasswordfield. i'm getting because shown in joptionpane output(just practice purposes).
eclipse not give me errors program not work correctly
okay here's flow of program. if jpasswordfield equal "admin". dialog box appear , display password (which "admin"). that's it.
i converted jpasswordfield string. still. there's no string displayed. there's no error displayed in eclipse also.
codes:
public class gui2 extends jframe { private jbutton shut; private jpasswordfield field; public gui2(){ super("display password"); setlayout(new flowlayout()); field = new jpasswordfield(10); add(field); display = new jbutton("display"); add(display); thehandler handler = new thehandler(); display.addactionlistener(handler); } class thehandler implements actionlistener{ string password = new string(field.getpassword()); public void actionperformed(actionevent event){ if (password.equals("admin")){ joptionpane.showmessagedialog(null, password); } } } }
the problem variable password
initialized when class thehandler
instantiated (new thehandler();
). @ moment password equals empty string. when type password , want display it, has not been refreshed.
solution:
class thehandler implements actionlistener { public void actionperformed(actionevent event){ string password = new string(field.getpassword()); if (password.equals("admin")){ joptionpane.showmessagedialog(null, password); } } }
this way, each time click on display button (the actionperformed method call) retrieve current value of jpasswordfield
.
d. krauchanka right isn't why can't text type in field. should consider advice !
Comments
Post a Comment