Jump to content
  • Advertisement
Sign in to follow this  
cignox1

[java] waiting for a window to close

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!