I have been getting away with bash scripting along with some java (console) and python for my programming needs. Figured it's about time I tidy up my work into some graphical environment and I stumbled on a basic task that kept me hang for hours now.
The main function calls a login JDialog before starting the main app window. The JDialog instance runs a basic auth check (simple if statement) and sets a class field in the login dialog as true/false accordingly. I would love to retrieve in the calling class the result of this check and the username typed in the textbox. Seems next to impossible.
So, what is the optimal way of creating a new instance of a window (whatever is more suitable frame, dialog...) and be able to retrieve some class fields? Forms I have in mind are a little bit more complex than a yes/no answer. I should be able to bring up a login window, a settings window, or a form with parameters for a report.
Edit: Some code to sample the problem.
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login login = new Login();
login.showLogin();
System.out.println(login.getTheText());
Main frame = new Main();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
}
}
public class Login extends JDialog {
private JTextField txtTest;
public void showLogin() {
try {
Login dialog = new Login();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTheText() {
return txtTest.getText();
}
public Login() {
setModal(true);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
txtTest = new JTextField();
txtTest.setText("test");
txtTest.setBounds(112, 141, 86, 20);
getContentPane().add(txtTest);
txtTest.setColumns(10);
}
}
I set the text field txtTest to "test" at creation. I keep on receiving the original "test" value in Main, no matter what I type in the box.
Make sure that the JDialog is set up to be a modal JDialog which can be done by using an appropriate JDialog constructor or via a setter method. Doing this means that when you set it visible, the calling code stops and does not resume until the dialog is no longer visible. Once that happens the calling code restarts right after it stops. You can then query your dialog object for the value held by its fields.
For a concrete example, please see my answer to a similar question here.
You're not querying your JDialog after it returns. i.e.,
System.out.println(login.getTheText());
String text = login.getTheText();
Main frame = new Main(text); // and pass it into the other class.
This is already all explained and demonstrated in my link. Please read it. Also:
setVisible(false)
on your main GUI JFrame for an unknown reason.