tags:

views:

999

answers:

2

I have a closeWindow() method which uses dispose() for the current JFrame to close down. When I show the window again, the controls (textboxes, lists, tables etc.) still have their previous values in place that were there when I dispose():d the frame... Why is that? Is there another way to completley close and clear a frame?

This is the code that another JFrame uses to show the other window, am I doing something wrong here?

@Action
public void showAddProductToOrderView() {

    if (addProductToOrderView == null) addProductToOrderView = new AddProductToOrderView(this);
    addProductToOrderView.setVisible(true);
}
+3  A: 

The simplest thing to do would be to re-create the whole frame (using its constructor) before using show() to show it again. That will give you a whole new set of components, assuming that the constructor creates and places them.

Carl Smotricz
+3  A: 

Disposing a window will not clear its child text components. Dispose will release native resources. The javadoc for java.awt.Window also states:

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

As suggested by others, create a new instance each time instead. If that's to expensive I believe your best option is to clear sub components when the view becomes visible, e.g. by overriding setVisible.

EDIT: Remove the null check to create a new frame each time.

@Action
public void showAddProductToOrderView() {
    addProductToOrderView = new AddProductToOrderView(this);
    addProductToOrderView.setVisible(true);
}

I don't know about the rest of your code, if there's something else depending on the frame being reused. For example, if you have attached listeners, ensure they are unregistered to not leak them.

Samuel Sjöberg
Thank you! If you look at the code above, doesn't that create a new instance each time if the variable is null? I suppose that dispose() doesn't render the variable null then? How can I clear out that variable each time before creating a new instance?
Johan
The variable will not be set to null unless you're doing it yourself somewhere in your code. Disposing the frame will only release its native resources (the WindowPeer etc), it will not affect references held to the object. Remove the null check to create a new instance each time.
Samuel Sjöberg
It works! Thank you so much!
Johan