How to delete a JFrame when another is called - java

If I've created a JFrame with 2 JButtons in it. Both buttons call another JFrame through actionPerformed(ActionEvent event) (so 3 JFrames in total, the main one and the two which are called from it).
I'll name the JFrame with the JButton's main, and the two called JFrame's frame1 and frame2.
I call frame1 from one of the JButton's in main. Then I press the second JButton which calls up frame2.
I want frame1 to be closed automatically when frame2 is called and vice-versa.
I've looked for a solution and not been able to find one but I'm hoping it's fairly simple.
Thanks, help is appreciated.

I wouldn't recommend using multiple containers. Instead, use an appropriate layout manager (e.g. CardLayout). This way you'll have a single container with multiple views.
...deja vu...

Related

How to stay in the same frame with Java swing

I am trying to make a Quiz game and my idea is that i have a main frame class with buttons and a label like this
So when I click the Start button i want to stay in the same frame just delete all these buttons and label and replace them with the question and answers to it. How can I accomplish this? Read something about repaint() method and removeAll() but what is the best practice. I come up with this(dunno is it the right thing to do in the situation) but call removeAll() in the actionListener of the start button and create the new frame in this listener? Or can I make another class e.g called StartFrame and then in the actionListener of the start button call removeAll() and then MainFrame.this = new StartFrame() assuming that my class holding the initial state of the game like in the pic is called MainFrame

Closing/Disposing JFrame that's been instantiated by a parent JFrame with an 'exit' button

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.
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.

How to properly hide a JFrame

I have a very simple JFrame window that contains one button: No.
In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.
I have two questions:
Do I really need to System.exit(0); in the above case?
If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
use CardLayout
if is there real reason for another popup container
use JDialog with parent to JFrame, with setModal / ModalityTypes
create only one JDialog and to reuse this one JDialog by getContentPane#removeAll()
use JOptionPane for simple users interaction
put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()
setVisible will cause slowdown
dispose will cause slowdown
System.exit will close entire JVM
Therefore, you should reuse a single JFrame or JDialog.
In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.
Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).
Note: The default option for JFrame is HIDE_ON_CLOSE.
You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.

JFrame close issue

I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it's not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this: getWindowAncestor(Component c). Then you could call dispose() on the window returned.
i.e.,
Window win = SwingUtilities.getWindowAncestor(myJPanel);
win.dispose();
Note that this is fine if you're using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.
The button is already on the frame (on the upper right, here). Just call JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) when constructing it and it will work as the user expects.
Other tip
Don't extend frame or panel, but instead just create and use them.

How can i display new GUI based on menu click

I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}

Categories