0

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

2
Contributors
4
Replies
14
Views
6 Months
Discussion Span
Last Post by cproger
Featured 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(); }); Read More

  • 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 … Read More

1

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();
});
0

Frame otherWindow = // a reference to the other window

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

1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.