0

I'm trying to achieve the following: Show login window, then from factory to determine what GUIclass to open for instance (admin/dev/tester).

I cannot manage to make the second window pop up; I tried with dispose it closes the program but not runs the main to the end.

main:

public static void main(String[] args){
        LoginGui loginGuiWindow = null;
        DeveloperGui devGui = null;
        TesterGui tesGui = null;
        UserCntrl uc = new UserCntrl();
        try {
            loginGuiWindow = new LoginGui(uc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.print("Checking instance of: ");
        if(loginGuiWindow.loggingResult > 0){
            System.out.print("Checking instance of: ");
            if (uc.user instanceof Developer) {
                System.out.println("is instanceOf developer");
                devGui = new DeveloperGui(uc);
            }
            if (uc.user instanceof Tester ) {
                System.out.println("is instanceOf tester");
                tesGui = new TesterGui(uc);
            }
        }
    }

LoginGui:

btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int res;
                String nick = textUser.getText();
                String pass = textPassword.getText();
                if((res = uc.handleLogin(nick, pass)) > 0){
                    loggingResult = res;
                    uc.handleUi(res);
                    frame.setVisible(false);
                    frame.dispose();
                }
                else{
                    JOptionPane.showMessageDialog(frame,
                        "Wrong username or password.",
                        "Logging error",
                        JOptionPane.PLAIN_MESSAGE);
                }
            }
        });
James Oravec
  • 19,579
  • 27
  • 94
  • 160
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
  • i dont see where are you showing the window, are you doing it in the constructor? – Dima Apr 23 '13 at 14:40
  • Have you tried to debug the code? does `Main Method` waits at `loginGuiWindow = new LoginGui(uc);` till `loginGuiWindow is closed`? – rahul maindargi Apr 23 '13 at 14:41
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org). Currently, all you will get are random shots in the dark, guessing what your code is actually doing. – Guillaume Polet Apr 23 '13 at 14:51
  • 1
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Apr 23 '13 at 16:16

2 Answers2

2

when you are showing a window, its not a blocking call, try setModal(true) before you show it

Dima
  • 8,586
  • 4
  • 28
  • 57
1

Make sure LoginGui's dialog is modal.

If it's not, control will return to your original method after making it visible. loggingResult is never > 0, so the "Checking instance of" never runs. Then, when the user finally does log in, that code has already run, and nothing happens.

Making the dialog modal will essentially "pause" on the line where you open the dialog. When the dialog is closed, control will resume from there, which is what you want (in this case)

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60