• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

how to get 2 different actions when using frame.dispose() and when the user clicks the closing cross

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am currently writing a little program in Swing.
I have something I do not know how to do.

At some point in the program I have a window, let's call it window A. I want, if the user closes the window A by clicking on the closing cross, to close window A and to show the main menu window. This I can do.
To do that, I used a class that implements WindowListener. In that class, I defined the windowClosed method, in which I show the main menu window.

In my window A I set the listener and the default close operation.


The problem is, at some point in my code, I want to close window A and go to another window B, without showing the main menu window. And I have no clue how to do that.

So currently, if the user closes window A, it disposes the window A and shows the main menu window. I want that.
But if I use the dispose() method on window A, it also shows the main menu window. I don't want that.

Thanks in advance for your help. I hope I was clear.
 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to close window A and go to another window B, without showing the main menu window.  


Can you use an if statement to detect the condition where you don't want to show the menu?
if(condition) {
  show Window B
}else {
  show menu
}
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the class called Window (or JWindow)? Don't. Use JFrame or, better still, a dialogue. You will have to return something from the dialogue to tell its parent frame whether you want the menu visible or not.
Why do you want menus appearing and disappearing? That will give your display a very peculiar appearance.
Are you extending display classes? That is usually a bad idea; your program IS NOT A JFrame, but is HAS A JFrame. You should usually only use one frame per program; everything else is better as a dialogue.
 
Joss Paz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I found the solution.
I replaced this code

with this code

BTW the window is a JFrame, why would you assume otherwise?
 
Campbell Ritchie
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joss Paz wrote:. . . BTW the window is a JFrame, why would you assume otherwise?

Because there is a class called Window (and also JWindow).
 
Where does a nanny get ground to air missles? Protect this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic