Closing newly opened JFrame - java

I'm developing a desktop application using NetBeans IDE.
1) I'm opening a new JFrame after a button click. While I'm closing any of the opened frames, both are getting closed.
2) And I want to update a new Tabbed JPanel on a JFrame after button click.
Thanks in advance.

Probably you are using the wrong argument. I guess for the JFrames that you want to close without closing the entire application you are doing:
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
While to actually close only the desired frame without closing rest of the frames you need to do:
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

1) Why did you create lots of Top-Level Containers on Runtime, create only one JFrame and other would be JDialog(Modality) or JWindow(un-decorated), re-use that,
2) really no battery included in my Magic Globe today, for better and sooner hepl you have to post here SSCCE

I want to update a new JTabbedPane on a JFrame after the button click.
You can just update via a reference to the tab's JPanel and call repaint(), but sooner or later you should consider the Model–View–Controller pattern.

Related

Open elements and struct of jFrame in another like a menu

i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes

Closing a JFrame from a JPanel

I have a JFrame with a login(JPanel) which can be selected from a menubar. The menubar also shows options depending on users roles. When a user first logs in an instance of the JFrame is created, my problem comes when i select login on the menubar and instanciate en new JFrame since i obviously have 2 JFrames open and i can't find a way to hide or close the previous JFrame from the JPanel. Is there any way to hide/dispose all open windows right before i instanciate a new JFrame or any other possible solution? Thanks in advance for your time!
Start by taking a look at The Use of Multiple JFrames, Good/Bad Practice?.
Instead of using a second JFrame, using a JDialog of some kind to show the login window. A modal dialog will block the caller when the dialog is made visible, allowing the code to continue once it's closed. This is very helpful for gain information from the user...
For example, use this to gather the credentials (and possibly authenticate) the user and allow the caller to extract the results when the dialog is closed.
Have a look at How to Make Dialogs for more details

Make JFrame display other JFrame without opening new window

I'm not sure if this has been answered or the correct way of phrasing it, but have had no luck in my search. I have 4 JFrame guis all in their own classes: a main gui and 3 others. I want to know if it is possible to display the other guis inside the same window without opening a new window and setting the first window to a false visibility? I'm able to call the other JFrames and make them display through a series of actionlisteners, but they open another window, making me have to setVisible(false) the gui window. I want to be able to have all the guis display in the same window without opening/closing windows. Thanks
You should not be creating separate frames. Just create separate panels and swap the panels.
See the Swing tutorial on How to Use Card Layout for more information.
Also, if you ever do need more than one window, you should be using a JDialog for the child windows. An application should only have a single JFrame.

Java: create second frame while clicking on button of first frame

In my application one frame is there on which one button is there. If this button is clicked then second frame will open and it will take some data from the client. And after submitting (Submit Button on Second frame) these data, frame will close.I use JFrame for first frame.
Now what I use for the second frame.
These type of question previously asked on this site but lot of people say that use JDesktopPane, JDialog and other things. I am confused so please clearly specify what is the right way. currently I am using JFrame for first and second frame. But I know this is bad idea to use two Jframe in one application and second frame also not work in that way which I want.
If your answer is to use JDialog then please mention how I customize this.
I am attaching the look of second frame which help you guy to understand what I want in my application.
Use JDialog and make it modal. So when you call it you can process all the results just after cloging it in the same method.
If you are using two frames then in the second frame use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Swing: Click on a button in a frame, show another frame with different components

I am developing an application which needs to pop up new JFrame B with different components, when I click on a button on JFrame A. How do I achieve that?
I don't want to use the tabs.
Use a JDialog , problem solved!
See this java tutorial for more help : How to Make Dialogs
I'm not sure why no one has suggested CardLayout yet, but this is likely your best solution. The Swing tutorials have a good section on this: How to use CardLayout
In a nutshell (a simple solution), you register a listener with the JButton and then have the listener perform the tasks you want it to perform:
setVisible(true) for one frame.
setVisible(false) for the other one.
Regards!
One way to approach this would be to create another jFrame and then add a listener onto your button like so:
jFrameNew.setVisible(true);
This way you have a whole new frame to work with. If you want to just have a pop-up message you can also try using the jDialog frames.
Depending on which IDE you are using...for example Netbeans has a gui that makes designing interfaces slightly easier, so you can test out the different frames.

Categories

Resources