Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Greenhorn
Posts: 7
  • 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: 4256
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: 72478
315
  • 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: 7
  • 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: 72478
315
  • 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).
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic