[java] waiting for a window to close

Started by
6 comments, last by Vyper_uk 18 years, 8 months ago
Well, everytime I begin a new java project, it seems as I have to study it again from scratch. My application starts as a login window. Once you insert the username and the pwd you can enter the main window. Since I want that from this window one can restart the program (without closing it), I would like to do the following: -the application starts without gui and creates the login panel and wait for it t close. -Once it closed (i.e. the user pressed 'login') the application starts the main window, waiting for it to be closed by the user (in some way). -If the user likes to restart the app, this window closes and the control returns to the non-gui waiting side of the app, that (since it is a loop) creates again the login panel. My question is: how to make the program wait for a window to close? [Edited by - cignox1 on August 10, 2005 6:31:15 AM]
Advertisement
window closing events are handled with a java.awt.event.WindowListener.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Thank you for the reply, but I already know that (or I'm missing something?). What I don't know is how can I make the main loop suspend till the window closes:
    public static void main(String[] args)     {     while(true)     {        window l = new window();//the loop stops till window closes        window m = new window();//When this one closes, the loop restart.     }       }


My solution would be to add a flag to the window class. When the window is opened, the flag is 1, then it is set to 0. The loop uses something like Pause(100) and polls the status of the window till it find the flag set to 0, and then shows the second window, doing the same thing.
This method should work, but I don't like it because the loop would steal cpu time.
Is there some other way?

Thank you.
class TopClass implements WindowListener{private Window l, m;private TopClass(){   l = new Window();   l.addWindowListener(this);   m = new Window();   m.addWindowListener(this);   l.setVisible(true);}public void windowClosed(WindowEvent e){   ((e.getWindow() == l)?l:m).setVisible(true);}public static void main(String[] args){  new TopClass();}}


something like that? I have left out the other methods of the window listener interface, naturally you'll need to include them as well.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

mmm...not really, because if the second window was closed with a specific button, then the first window should be created again, and then the second and so on. I was thinking to something like what we do with MS MFC when we create modal windows:
window A = new window();
A.DoModal();
.
.
.
When the program reaches the DoModal() call it breaks till the function returns.
I could be totally wrong, but wouldn't the easiest way to do this be to just add custom listeners to the buttons in the first window? The login button on the first window could have an ActionListener that processes the login details and, if they are valid, creates the main window and closes the login window. You could add custom listeners to the other things in the window to do various other tasks (eg a WindowListener that closes the entire program if the user closes the window or whatever). Then you just need another ActionListener on the restart button or whatever you have in your main window, which when activated closes the main window and makes another instance of the login window.

I think loops are the wrong way to think about this, think in terms of the actions your users can perform and the desired effects of those actions.

Here is an example of the kind of thing you'll be wanting:

//various imports...public class LoginButton extends JButton {        private JFrame loginWindow		public LoginButton(JFrame loginWindow) {                this.loginWindow=loginWindow;		setText("Login");		addActionListener(new LoginListener());	}	private class LoginListener implements ActionListener {			public void actionPerformed(ActionEvent e) {			//process login details                        if(loginValid) {                             //close the login window, you'll have to look up the method yourself                             //make the main window                               MainWindow m = new MainWindow();                         } else {                             //print a login error message somewhere                         }		}	}}


[Edited by - Vyper_uk on August 11, 2005 7:20:14 AM]
Thank you Vyper_uk. I thought to that solution but as I said I've not a big experience with java and every project I make I have to learn it again :-(, so I was not sure if this method would have been a good design or not. But it should work, so I think I will go with it!
No probs :) the best way to do guis in Java that I've found is to have the main class just create the window and add actionlisteners for your various functions and buttons. The actionlisteners then do all the processing etc, no loop is required

This topic is closed to new replies.

Advertisement