2

I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.

Ok anyways moving on.

I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.

What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.

Any advice?

EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.

Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.

I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.

That is my issue.

Eric
  • 373
  • 1
  • 3
  • 14
  • http://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe – BitNinja Mar 02 '14 at 03:20
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Mar 02 '14 at 03:20
  • Tried that method codeNinja. Got no results. – Eric Mar 02 '14 at 03:21
  • I wish I could feasibly utilize modal dialogs but my program needs to stay on one window. In any case it's only going to be a max of two JFrames open at one time, max. – Eric Mar 02 '14 at 03:24
  • 1
    You last comment makes no sense. If you think you can use two JFrames, there is no reason you can't use one JFrame and one JDialog. A dialog does NOT have to be modal. – camickr Mar 02 '14 at 03:29
  • Tip: Add @codeNinja (or whoever, the `@` is important) to *notify* a person of the new comment. And while on the subject, who were you addressing in the 2nd comment? Me? The article lists more ways than just 'dialog' to get around multiple frames, but your comment doesn't really make sense to me anyways, since by having 2 frames, you break the *"my program needs to stay on one window"* requirement. It seems it needs a `CardLayout`. – Andrew Thompson Mar 02 '14 at 03:31
  • @AndrewThompson . Apologize I'm just set in my ways currently. I'm looking into trying a JDialog inlue of the JFrame but otherwise really don't wish to completely undo all the coding I had to go through to get my panel's components in the write layout. And don't really use stackoverflow that often, was unaware of the format. – Eric Mar 02 '14 at 03:34
  • *"I have a button on the panel that is an exit button."* I think that is at the heart of the problem. Take it out. A component should not be involved with the appearance/disappearance of its top level container. That goes against user expectations. If the panel were instead displayed in an option pane, the user could dismiss it by hitting the escape key (or clicking the `x` button, ..or by activating any of the buttons at the bottom..). – Andrew Thompson Mar 02 '14 at 03:36
  • `don't wish to completely undo all the coding I had to go through to get my panel's components in the write layout.` - there is no difference between a JFrame and JDialog. They both have content panes and use layout managers. The only difference is new JFrame(...) vs new JDialog(...). – camickr Mar 02 '14 at 03:37
  • *"don't wish to completely undo all the coding I had to go through to get my panel's components in the write layout."* **That is the problem you should solve.** A GUI layout should be logical and robust. – Andrew Thompson Mar 02 '14 at 03:38
  • @AndrewThompson Kinda saw the backlash about my exit button coming. – Eric Mar 02 '14 at 03:38
  • common sense != backlash – Andrew Thompson Mar 02 '14 at 03:39
  • @AndrewThompson I don't wish to get into an argument but I don't really see the downside to having a button that exits on the GUI... Other than 'wasted coding' – Eric Mar 02 '14 at 03:46
  • *"I don't really see the downside to having a button that exits on the GUI..."* Your users probably will, but that is not my problem. So ..good luck with it. :) – Andrew Thompson Mar 02 '14 at 03:50
  • 1
    @Eric, Show me an application by Microsoft or Apple (or any other large company) that has a button to exit the application. These companies spend millions of dollars to develop consistent UI's to be used by all users. Close buttons are generally used for dialogs so the dialog can close and go back go the main frame. Learn by example from what other do. – camickr Mar 02 '14 at 03:54

1 Answers1

5

See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.

camickr
  • 321,443
  • 19
  • 166
  • 288