Making multiple frames in a single program - java

I always wondered on how can I make a program with multiple JFrames. I mean I just want one class to handle all the GUIs and stuff but how can I effectively do this? A lot of tutorials say that we make JFrame by inheriting from JFrame. But what If I want many frames?
Ex:
Title of Application in one frame with some options
Menu is one frame
Main working application is one frame
Like in a game.
But I am not sure if I am pertaining to JPanel? I am completely puzzled with the 2. I just want one un-moving frame but basically the content of the frame is changing.
When I click START for example, it will change to the gaming style of frame.

you are looking for a JFrame with a CardLayout. Some background:
A JFrame is the physical window. It comes with a title bar and three buttons: minimize, maximize, and close. Think of this as a picture frame.
A JPanel is a "content holder" of sorts. Typically, you put your other components (buttons, animations, whatever) on a JPanel, and then slap that JPanel into a JFrame. Using our picture frame example, a JPanel would be the photo paper you put in the picture frame. The other components would then be the actual contents of the picture itself, and what you have at the end is a nice picture...or in your case, an application.
Setting the JFrame to utilize a CardLayout essentially lets you have multiple JPanels inside the same JFrame at once, while still only showing one at a time. So for your application, you would have (at least) two JPanels: one for the menu, and one for the game. When the app starts, you show the MenuPanel. When the user clicks "start", you switch to the GamePanel. The MenuPanel will be put in the background and will be inaccessible until you call it to the foreground again.
If, on the other hand, you create multiple JFrames, you will have two or more physically separate windows that can be dealt with individually. This can actually be kind of cool for game development. Although it takes more time to build and link the GUI for the second window, you can then have that window affect game settings in realtime (rate of fire, bullet strength, player speed, etc.)

I think that what you are after is the Card Layout:
A CardLayout object is a layout manager for a container. It treats
each component in the container as a card. Only one card is visible at
a time, and the container acts as a stack of cards. The first
component added to a CardLayout object is the visible component when
the container is first displayed.
You can see how it is used here.
This layout manager allows you to manage situations where your frame needs to be shared across various functions. In your case for instance, you could have a functionality to handle the settings section of the game and another one to handle the actual game itself.
You could then use the manager to switch between these particular items.

you can also use Desktopane() and InternalFrame() for multiple frame.
Internalframe quite similar to Jframe but it need to setVisible(true) or show() everytime.

Which ever IDE you are using, you can create multiple JFrames in the same package, and have separate codes for each of them.
If you want to link each frame, you will have to create instances from each JFrame. for example, if when the button is pressed, we need to invoke a new Frame (that we have already created)
NewJFrame1 frame1=new NewJFrame1();
frame1.setVisible(true);
then you can decide what to with your current JFrame.
eg : (Hide, Close)

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

How to create dynamic wizard?

I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.

Is it possible to switch from screen to screen by just using one JFrame?

Is it possible to switch from screen to screen by just using one JFrame?
Do i need to create another JFrame or use JPanels and turn it on/off on button presses to create an illusion of jumping from screen to screen?
Or is there any other more efficient way to do this with other containers like
JDesktopPane, JLayeredPane, JInternalFrame and etc in netbeans ide?
What is the purpose and difference in usage of Swing Containers & Swing Windows?
When to use Containers and when to use windows?
By the way, i'm trying to build an Inventory System App Interface.
you can use JInternalFrame which is make how many child Frames you want in a JFrame and you can do also iconified,closable,resizable ... by make them true in child Frames until you can use JPanel instead of JFrame and switch between them(child Frame's) by just one click!
Edited------------
For Example:
JInternalFrame jInternalFrame = new JInternalFrame("Hello!",true,true,true,false);
jInternalFrame.setSize(160,200);
getContentPane().add(jInternalFrame);
jInternalFrame.add(panel);
jInternalFrame.setVisible(true);
And when you click the back button, it will return to the initial menu without popping out a dialog or changing the size or location of the window.
Sound like you can use a CardLayout. Read the section from the Swing tutorial on How to Use Card Layout for more information and examples.

How to add Transitions (Fade, dissolve etc ) in Java GUI

I am trying to make a game for my semester project. I want to show a transition when user clicks on options button of my game menu or when user clicks on credits button. I want to show transition when one panel replaces another. Is it even possible? I am using java swing library.
You should use a CardLayout to swap views (JPanels) in your GUI. To get a fade effect is not the most simple thing to do, but it can be done with a little work. I've done it successfully here where I create a special class called SwappingImgPanel that extends JPanel, and that fades one image into another using a Swing Timer. Specifically, the program does this:
The program adds all the swapping components to the CardLayout using JPanel.
It also adds a single SwappingImgPanel, a JPanel created to draw two images, one of the component that is fading out, and one of the component that is fading in.
When you swap components, you create images of the two components, the one currently visible, and the one that will next be visible.
You send the images to the SwappingImgPanel instance
You call swap() on the SwappingImgPanel instance.
The SwappingImgPanel will then draw both images but uses a Swing Timer to change the Graphic object's composite value. This is what causes an image to be partially visible.
When the SwappingImgPanel's Timer is done, a done() method is called which sets the SwappingImgPanel's State to State.DONE.
The main GUI is listening to the SwappingImgPanel's state value, and when it achieves State.DONE, the main GUI shows the actual next component (and not an image of it).

how to start, developing swing based application with few panels with next buttons for each

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each one in separate .java file inside same package.
These two frames represents 2 screens (panels) of application. When Next button in first frame is clicked, it should move to second frame.
When I checked, these two classes are having main method, I think it should be correct way for creating applications. there should be only one main method.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
As I'm beginner, Can somebody suggest me in how to start up with these kind of applications? what are the guidelines that need to be followed? And please help me in finding documentation related to starting up with the development of such applications.
After going through the answers, My comments are:
I used the following code to go to next panel from first panel, but didn't worked.
private void gotoNextPanel(){
// jPanelFirstScreen.setVisible(false);
JPanelSecondScreen jpanelSecondScreen= new JPanelSecondScreen();
jpanelSecondScreen.setVisible(true);
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
upgradeUtilityGUI.removeAll();
validate();
repaint();
// upgradeUtilityGUI.add(jpanelSecondScreen);
upgradeUtilityGUI.getContentPane().add(jpanelSecondScreen, "card2");
jpanelSecondScreen.setVisible(true);
validate();
repaint();
}
I'm using netbeans, and 've added two panels to the cardlayout of frame. And when I use the above code to change panels, Nothing is happening, the first panel is still appearing. Can somebody tell me, how to write code for moving from one panel to another when both the panels 've been added to cardlayout of jFrame ?
Use a CardLayout, as shown here (and one frame) as mentioned by others.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
Make the panels public access level and they will be available from other packages.
One problem in that code snippet is implied by the line:
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
It goes out of scope before ever being added to a container. Also, their should be no need to remove anything when adding a new card to the layout, and no need to call repaint().
If your application is as simple as having only two panels you shouldn't create two JFrames. You should create a JFrame with two JPanel each of them contains the neccessary information for you. If you are ready with your first panel you can call setVisible(false) on it, and call setVisible(true) on the 2nd frame. It is the one of the most easy-to-understand solution.
But, it only depends on you if it is good for you or you would like to use some more detailed solution.
Don't use two or more JFrames, nor with separated and compiled Jar files, this is road to the hell, better would be look at CardLayout,
What you should do is have a single JFrame for the application, then you add and remove JPanels as you want to move between screens.
Each of your JPanels should basically have the following...
1. A JButton called "Next"
2. A ButtonListener for each button, that tells the JFrame to load panel2, panel3, etc.
As part of the ButtonListener, you basically just want to call something like JFrame.removeAll() to remove the existing panel, then JFrame.add(JPanel) to add the next panel.
By having 1 JFrame, you also only have 1 main() method.

Categories

Resources