Hi.
I have a LoginForm.java, and then when the user enter the right username and password, then another file called "DictionaryForm.java" opens and the LoginForm.java, closes. How can I do that ???

see the LoginForm.java:
http://www.daniweb.com/forums/thread119702-2.html

Dani AI

Generated

Practical pattern: keep the login UI as a short-lived dialog and move the program entry point into a small launcher. Create the login as a modal dialog so it blocks until the user finishes authentication, then create and show your Dictionary window from the launcher. Running GUI creation on the Event Dispatch Thread avoids subtle bugs, and choosing the right default-close behavior for each top-level window prevents the JVM from exiting unexpectedly. (docs.oracle.com)

Example launcher workflow (minimal):

public class AppLauncher {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      LoginDialog login = new LoginDialog(null, true); // modal dialog
      login.setLocationRelativeTo(null);
      login.setVisible(true); // returns when dialog is hidden/closed
      if (login.isAuthenticated()) {
        DictionaryFrame dict = new DictionaryFrame();
        dict.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dict.setLocationRelativeTo(null);
        dict.setVisible(true);
      }
    });
  }
}

Implement the dialog so its OK handler sets an authenticated flag and then hides the dialog (so the launcher continues). This approach follows advice to separate the main method from the login UI and keeps LoginForm as a reusable component. The Oracle tutorials explain modal dialogs and the recommended EDT usage. (docs.oracle.com)

Troubleshooting notes: if the launcher does not resume, verify the dialog is actually modal and that the handler calls setVisible(false) (or otherwise closes the dialog). Use DISPOSE_ON_CLOSE for temporary windows and EXIT_ON_CLOSE only for the final application window. If behavior is still wrong, run the UI creation under SwingUtilities.invokeLater and check for exceptions printed to the console. confirmed this separation fixed the original problem. (docs.oracle.com)

Recommended Answers

All 7 Replies

what do you mean by: the LoginForm.java closes? do you mean that it's a JFrame that you want to disappear?
in that case, use eiter the hide() or dispose() method for the LoginForm frame, and show the new one

As I put the link, I have the LoginForm.java, which is not directly JFrame, but at the runtime, it creates a Form and asking for username and password,

and then I create a new Jframe Form, and named it DictionaryForm.java.

and in the LoginForm.java, if the username and password is correct it display a message, but I need it displays the DictionaryForm.java instead, and also the LoginForm.java should be closed.

I dont know what to do?

As I put the link, I have the LoginForm.java, which is not directly JFrame, but at the runtime, it creates a Form and asking for username and password,

and then I create a new Jframe Form, and named it DictionaryForm.java.

and in the LoginForm.java, if the username and password is correct it display a message, but I need it displays the DictionaryForm.java instead, and also the LoginForm.java should be closed.

I dont know what to do?

public class LoginForm extends JFrame

sure looks like a JFrame to me ..

what you can do (and I'm not saying it's the most elegant or efficiënt way, just got to work here :))


in the LoginForm.java, you add the line

this.dispose();

to that part of the if-structure where the login succeeds

on the place where you call the loginFrame, you check wether or not it is still "active", or "visible", or you place a trigger in your LoginFrame.java, right before the dispose(), and after this checks out, you call the DictionaryForm

Thanks for the reply.
Here I attached the source.

well ... first tip, LoginForm is not supposed to be or contain your main method, it's a tool that's to be used, nothing more.

just put the main method in a new class and use that to call the LoginForm and the DictionaryForm.

when you've done that, re-read the posts above and put those thoughts into a bit of code.
for what I can see in the code you attached, you haven't tried to code it yet, so it's nothing but natural that it doesn't work yet :)

thanks very much,
it is working well now.

if it works, mark the thread as solved, it makes it easier to see which problems still need solving :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.