WilliamShatner
WilliamShatner

Reputation: 926

Enabling and disabling button based on whether frame is open

I have a button that opens up a frame. Is there a way to enable/disable a button depending on whether the frame is open?

Let's say, if the frame is open, I would like the button to be button.enabled(false). But as soon as the frame is closed, I would like to change it to button.enabled(true).

In my actionPerformed method of the button I do this

JFrame testFrame = new JFrame();
testFrame.setSize(100,100);
testFrame.setVisible(true);

However, I don't want to open up more than one of these frames at a time. So while the frame that is created by the click of the button is open, I want the button disabled until the frame is closed. (Even if the frame is not visible, I still don't want to allow another one to be opened)

Upvotes: 3

Views: 3502

Answers (3)

dann.dev
dann.dev

Reputation: 2494

If your intention is to have the button only create a single window, I would suggest rather then disabling enabling the button, you have it point to a single JFrame, so somewhere in your code, have a class variable:

JFrame myFrame = null;

Then the button code should be:

If (myFrame == null) {
    myframe = new JFrame();
    //set up the frame etc
    myFrame.setVisible();
} else {
    myFrame.setVisible();
    //code to bring focus to myFrame
}

To be honest I can't quite remember the best way to bring focus to the window, but here are a few links:

How to bring a window to the front?
http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2006-06/msg00152.html

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You probably shouldn't be opening a dependent window as a JFrame from another GUI. Likely you're much better off opening a dialog such as a modal or non-modal JDialog or JOptionPane. Please understand that either of these two critters can hold very complex GUI's. For instance, please have a look here for an example: how-do-you-return-a-value-from-a-java-swing-window-closes-from-a-button

Also if your dialog variable is a field of your class, then it is created only once and you can't have two of these windows displayed even if the button to display it is pressed more than once.

Your code could look something like...

// testDialog is a JDialog field. and this line is called in 
// the class constructor.
JDialog testDialog = new JDialog(theCurrentJFrame, "Dialog Title", false); // true 
                                                                    // if modal

// this line is called in the button's ActionListener.
testDialog.pack(); // Never set the size of your GUI's. 
                   // Let the layout managers do this for you.
testDialog.setVisible(true);

Upvotes: 3

Thomas
Thomas

Reputation: 5094

Add an onClose listener that sets the button to enabled when the user(or program) closes the frame. Likewise add a line to disable the button in the call that opens the frame in the first place.

Upvotes: 3

Related Questions