How can I disable a JFrame while another one is opened

760 views Asked by At

I have a JFrame that has some JLabels, and when I click any JLabel, it opens another JFrame but I want the other to stay disabled. First I did:

JFrame frame2 = new JFrame();
frame2.setVisible(true);
this.setEnabled(false);

Then I want to setEnabled(true) the 1st frame when I close the 2nd frame. What I did is giving the 2nd frame a JFrame attribute and giving a param JFrame in its Constructor, and with a WindowListener I use the method windowClosing() like this:

public class My2ndJFrame extends JFrame implements ActionListener, WindowListener {

    My1stJFrame frame;

    public My2ndJFrame(My1stJFrame frame) {

        this.frame = frame;
        //moreCodeWeDontNeedNow

    }

@Override
    public void windowClosing(WindowEvent e) {

        frame.setEnabled(true);

    }

It works but I dont know if this is good programming practice, so I would like to know if there's a better way and if this is not good, why. Thank you.

0

There are 0 answers