I am currently testing various ways of getting user-input. Today I was testing with the OnClosing(), which seem to work. My problem is that if I don't put anything in the while() - loop, it doesn't work. Its easier to show with code:
public MyClass() {
String myPassword= new Password().getpw();
}
ok. This is how I'm getting the string.
public class Password {
private boolean goon = true;
private JPasswordField jpassword = new JPasswordField ();
public Password() {
JFrame f = new JFrame("Type your password here, get it by closing the frame");
f.getContentPane().add(jpassword);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
goon = false;
}
}
);
f.setSize(400,65);
f.show();
}
public String getpw() {
while (goon);
return new String(jpassword.getPassword());
}
}
This doesn't work unless I put something random in the while()-loop.. let me show you. This way it works
public String getpw() {
while (goon) {
System.out.println("");
}
return new String(jpassword.getPassword());
}
Why is it not working with an empty loop?