2

I am developing an application where the valid user gets access to the main application. But a problem arises when I run main class. The LoginFrame and Main(Editor.java) Frame start simultaneously. I want to first validate the user and then direct the user to the main application. I am calling Loginform.java from my main application (i.e Editor.java)

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
             new Login().setVisible(true);
    {
         Editor x =   new Editor();
         x.setVisible(true);
    }
        }
    });
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • In it's present context, seems to me [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) is what you need. Regarding your Login thingy, what you can do is write a small code for validation inside the static block and validate the credentials in the same before the execution goes to the main method.. I guess this [example](http://stackoverflow.com/a/9107536/1057230) might can help you a bit. – nIcE cOw Oct 27 '12 at 12:28
  • See [this answer](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657) for lots of alternatives to using more than one frame. This seems it needs a modal dialog. – Andrew Thompson Oct 28 '12 at 04:49

2 Answers2

3

You could use a modal dialog for the login part i.e. a dialog that blocks until completed.

At the moment, both of your windows are (presumably) non-modal, so there is nothing to stop them both appearing immediately.

The alternative is to first show the login window, and only create the main window once the user has successfully logged in. Your code currently creates the Editor without checking whether the login was successful.

DNA
  • 42,007
  • 12
  • 107
  • 146
1

You could display the main window with all the menu items and toolbar buttons hidden or disabled and a modal dialog being the login frame, with the parent being the main window. Once the user logs in, the main window activates all its stuff. Otherwise, just let the user know he entered wrong credentials and close both windows.

Dan D.
  • 32,246
  • 5
  • 63
  • 79