• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dispose?

 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

in some java books I've read, I read that when closing a second frame that's on top of a main frame by using the x button in the upper right corner, a windowlistener event needs to be written to dispose that second frame. But I've noticed that when I just click the x on the second frame, it just closes the frame, not the main frame. This has me puzzled; do I really need to write a dispose for that frame?
 
Campbell Ritchie
Marshal
Posts: 52558
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have look in the API for JFrame#setDefaultCloseOperation(int) and you see you can set a default operation on a JFrame. The API lists the 4 possibilities. EXIT will close all frames and all other operations, and is probably too vicious for an application which needs file writing, database access, etc.

From what you say, you can probably simply use DISPOSE with that method.

More of a Swing-related question, so I shall move this thread.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

Though what I meant more is this:

Let's say I have a second JFrame on top of a main JFrame. I want to close the second frame without closing the app. A couple of books mention that the second window's x (close) button needs to be programmed to be able to close it. But what I noticed is that the window just closes by clicking x without programming it. So, is it really necesary to program the close button?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't you be better off having a JDialog sitting above the main JFrame rather than a second JFrame? Then there is absolutely no danger of closing the app when the dialog closes, you can better control modality type, and you can guarantee that the subwindow always sits above the main one.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, a JFrame is only hidden when you click on the X. Disposing it is better for memory management. Also, hiding will never cause the event dispatch thread to end - disposing may.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand. I suppose this also applies to a JDialog?
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the exception that JDialog does not allow EXIT_ON_CLOSE, yes.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This leads me to ask another question, though. I have a JFrame with a couple of buttons on it. I use a 1/3 of a MVC pattern on it - with that I mean that I implement VC, not the model. I think this also is called the strategy pattern. When somebody clicks on a button, an anonymus inner class in the "view" gets called and forwards it to the "controller". But now I want pop up a new JDialog box which also has a controller. My question is this; should the new JDialog and the controller be wholly instantiated within the method in the controller for the JFrame? Or should the be reference variables on a class level in the controller? I can't seem to figure out what is the best solution and any help would be appreciated!
 
Campbell Ritchie
Marshal
Posts: 52558
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have done better to ask your new question in a new thread. Nobody reading the contents page will know there is a new question.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I did put a new thread, it was one of the two you moved over here. But it wasn't getting any response and is drifting down the list. I thought I might as well put it in here as it seemed people regularly looked and responded.
 
Campbell Ritchie
Marshal
Posts: 52558
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you did create a new thread. I think I moved it; I always post "moving to" whenever I move anything. Please don't ask the same question in two threads.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!