0

I'm making one application which keep running as a Tray Icon when close, then I have this method.

public void ResetFrame(){
this.dispose()
new CreateTabs().setVisible(true);
}

It's pretty simple, CLOSE the frame then instance a new one, I need to instance a new one instead of just set visible because stuff. Then I'm calling this method multiple times inside the same class, the problem is when I try to call it from another class.

if(!createTabs.isVisible()){
            createTabs.ResetFrame();
            //createTabs.setVisible(true);
    }

I call that method everytime I click a button, I expect it to CLOSE the previous frame, destroy that instance of the object so I can call a new one with new properties.

The problem is: everytime I click the button it opens a new frame without closing the old one.

I have tried dispose(), finalize() and simple methods like that without any positive result.

Thanks beforehand for the help.

EDIT :

I'm sorry for my ignorance! The error is in ANOTHER class, the class where I call it.

Aparently the problem is while I instance the class:

private final CreateTabs createTabs = new CreateTabs();

I don't need a new CreateTabs(), what I need is to reference the already existing one.

Yayotrón
  • 1,759
  • 16
  • 27

2 Answers2

1

The way to close a Window is to use Window.dispose(). But be aware that you may not need to do this. If you reconfigure the components in your Window, you can use pack() to have the Window completely layed out again.

In the edit you made, if you change your private final CreateTabs ... instantiation to a public final ... you can access this window by classname.variable name from any context

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Hey! After checking my code again I notice that I went totally stupid at first, I just edited my question to clarify where my mistake is. If you wish, you can answer my new question :) – Yayotrón Dec 09 '14 at 19:10
1

Firstly you should check out this link for naming conventions (lower case function).

Im not 100% Sure what you are asking but I see the commented out part and know it would give you an error (if thats what you are asking about). When you call

public void ResetFrame() 

you say

this.dispose();

That means you are telling that instance to get rid of itself. Then you make a new instance and set it visible. Where you say

if(!createTabs.isVisible()){
    createTabs.ResetFrame();
    //createTabs.setVisible(true);
}

You told the instance of createTabs to get rid of itself so you cannot now tell it to be visible again. The new instance of CreateTabs you made in the ResetFrame function is not being referenced. If you want createTabs to be a "fresh" CrateTabs, you can say...

public CreateTabs ResetFrame(){
    this.dispose()
    CreateTabs newTabs = new CreateTabs();
    newTabs.setVisible(true);
    return newTabs;
}
...
if(!createTabs.isVisible()){
    createTabs = createTabs.ResetFrame();
    createTabs.setVisible(true);
}

If that is not what you were asking about just let me know.

707090
  • 102
  • 6
  • Thanks for the link, I just refactor my whole project. I got to fix my problem with that http://stackoverflow.com/questions/4717953/passing-reference-of-class-to-another-class answer and some other tricks, but that one is actually a better answer, just tested it out and worked in less steps, I had not thought of that, wise solution man. I can't vote up you, don't have enough rep :( – Yayotrón Dec 11 '14 at 13:02