3

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)

THelper
  • 15,333
  • 6
  • 64
  • 104
WilliamShatner
  • 926
  • 2
  • 12
  • 25
  • Frames? As in http://www.w3.org/TR/html4/present/frames.html ? If this is what you are getting at, I would strongly recommend using some other method entirely. – Andrew Apr 02 '12 at 20:31
  • 1
    Define open? do you mean if the window is created, or just open? For example you can create a JFrame, but not display it, you can also have a JFrame up but it could be minimized? – dann.dev Apr 02 '12 at 20:37
  • 1
    Is this AWT, Swing, Android, a different GUI library? This is key information and will change what the correct answer should be. – Hovercraft Full Of Eels Apr 02 '12 at 20:43
  • 1
    All, sorry for not being specific. I'm just talking about Swing. I'm more interested in learning how to do it and why it should be done that way rather than simply the code. Also, I added a edit to clear up some questions in the comments. – WilliamShatner Apr 02 '12 at 21:10
  • 1+ vote for responding to our request for more information. – Hovercraft Full Of Eels Apr 02 '12 at 21:26

3 Answers3

3

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.

Thomas
  • 5,074
  • 1
  • 16
  • 12
  • I think this is the way it should be done, however I'm unsure of how to do it. I'm a little confused as to how the listener works. Do I want to add the windowlistener to the frame and then override windowclosing? – WilliamShatner Apr 02 '12 at 21:21
  • Although I have already chosen an answer for the question, I'm still interested in your answer if you'd like to provide it! – WilliamShatner Apr 02 '12 at 22:27
  • Unfortunately, I guessed the wrong library and do not know how to do this in swing, but [http://stackoverflow.com/questions/7650990/java-jframe-intercept-exit-on-close-to-save-data](http://stackoverflow.com/questions/7650990/java-jframe-intercept-exit-on-close-to-save-data) might help. – Thomas Apr 03 '12 at 02:08
  • Thanks for the link, I'll also give this way a try. I'm curious to see how it works. – WilliamShatner Apr 03 '12 at 13:55
3

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

Community
  • 1
  • 1
dann.dev
  • 2,406
  • 3
  • 20
  • 32
  • I'm not too concerned about the focus of the component. Rather, I'm not wanting to allow the component to be created more than once. But once the component is closed, I'm wanting the user to have the option to open it back up. – WilliamShatner Apr 02 '12 at 21:13
  • Do you want information saved in there? Or a new one to pop up? – dann.dev Apr 02 '12 at 21:22
  • Just a new one to pop up. (Every time the button is clicked it displays a string and a random number, nothing that really needs to be saved. It is more for learning purposes) – WilliamShatner Apr 02 '12 at 21:23
  • That's what the above will do, it's an example of a singleton approach, as long as you are always opening it from the same class, you can never get more then one, may need to be tweaked to regenerate the random number though, you can see more here http://onjava.com/onjava/2003/08/27/singleton.html. – dann.dev Apr 02 '12 at 21:26
  • With that said though, what you probably want is a JDialog or JOptionPane as Hovercraft Full Of Eels has suggested. – dann.dev Apr 02 '12 at 21:27
  • Thank you for the example and the advice. I'll pursue what Hovercraft Full Of Eels stated and see how it works. Just a note, there is a syntax error in your if statement. When I have enough rep, I'll come back and upvote your answer as a thanks for your help and advice – WilliamShatner Apr 02 '12 at 21:44
3

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);
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Is this something to be concerned about if I'm not planning on making use of the information in the JFrame? (its just for display purposes). Also, as soon as I get enough rep to upvote, I'll be coming back and upvoting the answer you linked to. (and thanks for the tidbit about the pack method -- new to swing here :) – WilliamShatner Apr 02 '12 at 21:26
  • If you want this window to always display *above* the main GUI, then it should be a non-modal dialog. If you want only one window to exist, make it a field of the class rather than a variable local to the actionPerformed method. – Hovercraft Full Of Eels Apr 02 '12 at 21:27
  • Thanks for the advice, I'm looking to explore the JDialog option now (already tried dann.dev's example). If you don't mind me asking, why is the JDialog a better option than a JFrame? Can these dialogs hold tables as well? Looking at your example, it looks like they are quite capable of many things – WilliamShatner Apr 02 '12 at 21:51
  • @William: A JDialog can hold anything a JFrame can hold. The main strength of these is when you need a modal dialog -- a window that has focus and won't let the user back to the main application until the dialog window has been closed, but it's also quite useful for non-modal windows if you want that window to remain above the main application window. – Hovercraft Full Of Eels Apr 02 '12 at 22:24