Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SWING/AWT windows not "clearing"  RSS feed

 
Bryce V.
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building my first SWING/AWT java program... so i'm an extreme noob when it comes to some things. Well i've come across my first really perplexing issue.
At one point in the program, i call a funtion, which creates a window. To close the window ( in the function) i called "dispose();". Then, if i bring up another window, the jlabels, jbuttons, jtextarea's... are all in the new window.
How do i properly close down a window, so that the first window's objects don't polute the next window to pop up?
thanks in advance
Bryce
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dispose() hides the window and all of its components, and discards any native resources they were using, but if you call pack(), show(), or setVisible() on this reference everything returns to the state it was in before dispose() was called.

It sounds like what you want to do instead is build a new window every time, or at the very least call removeAll() to remove all the components in addition to dispose().
 
Bryce V.
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply. But i'm afriad that your solution is a little too good for my needs. As i have one class calling multiple functions( each function creates a new window). If i put the removeAll() anywhere in the class, after i close one window, when i call another, there is nothing there( including the window being of no size).
Is there a way to keep removeAll from removing everything in the class, and just keeping it local to one function?
thanks again.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you are having a problem with the difference between a class and an object.
If you make a new instance of the class (form) everytime, it will be blank.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using a Swing JFrame you should call getContentPane().removeAll() to remove the components from the content pane.
 
Bryce V.
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathan, that was the solution to my problem. And thanks for showing me how to use removeAll() properly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!