satnam
satnam

Reputation: 1425

Launch a function when calling DISPOSE_ON_CLOSE

So I have 2 JFrames - J1 and J2. J2 is launched by clicking a button on J1. Also when that button is pressed, all other controls (JTextFields and JButtons etc) on J1 are meant to be disabled. For disabling all the controls, we have a function called DisableControls(). This function is called when the button is pressed on J1 to launch J2.

So when J2 is closed using DISPOSE_ON_CLOSE, we want to call another function EnableControls(), so that all the controls are back in enabled state.

My question is - Is there a way to call a function -EnableControls() when the user presses the close button on J2?

Thanks

Upvotes: 1

Views: 62

Answers (2)

camickr
camickr

Reputation: 324207

So I have 2 JFrames - J1 and J2. J2 is launched by clicking a button on J1. Also when that button is pressed, all other controls (JTextFields and JButtons etc) on J1 are meant to be disabled.

Don't use two frames. Instead the second frame should be a modal JDialog so you don't have to worry about disabling controls on the parent frame.

See: The Use of Multiple JFrames: Good or Bad Practice?

Upvotes: 3

ApproachingDarknessFish
ApproachingDarknessFish

Reputation: 14323

You can use a WindowListener (documentation) to detect the close event and call your function:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


//in J2 constructor
addWindowListener(
    //WindowAdapter implements WindowListener and lets us only override the methods we need
    new WindowAdapter() 
    {
        public void windowClosing(WindowEvent e)
        {
            //Call your function here
            J1.EnableControls();
        }
    });

Assuming that EnableControls is a method defined for J1, you may have to maintain a reference to J1 in your J2 class if you aren't already.

Upvotes: 0

Related Questions