This might sound like it's very simple but I tried many things and it doesn't work.

When the user clicks the back button on a window it's supposed to also dispose another window.

I've tried using static.

if(Tester.backJButton.isEnabled())
        {
                this.dispose();
        }

Tester is the name of the class holding the back button.

Thanks

Recommended Answers

All 4 Replies

You will need the window with the back button to have a reference to the other window that needs to be closed.
Then you can call that window's dispose method...

JFrame otherWindow = // a reference to the other window

backButton.addActionListener(e -> {
     otherWindow.dispose();
     this.dispose();
});

Frame otherWindow = // a reference to the other window

What do you mean by referencing to the other window? Do you mean the class?

No, the instance of the class.
So if win2 needs to dispose win1 as well you can do something like

JFrame win1 = new Window1();
...
JFrame win2 = new Window2(win1); // pass a ref to win1 into win2 so win2 can call win1's methods

so Window2's constructor would look like

class Window2 extends JFrame {
...
private Window1 win1;

public Window2(Window1 win1) { // constructor
     this.win1 = win1;
}

now any method/event handler in win2 can call win1's methods to dispose() it or whatever

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.