Open elements and struct of jFrame in another like a menu - java

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

Related

Revamp Panel Based UI to JTabbed Panes

I have developed a simple music player in Java which can play any given playlist or simple mp3 song.
For now i have worked all things out in plain JPanels. GUI doesn't look neat.
I need to revamp the GUI using tabbed pane. How this can be achieved using existing JPanels without affecting current functionality?
Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
Because a tabbed pane provides its own GUI, using a tabbed pane is simpler than using the CardLayout class.
"Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?"
It really depends on your preference of the look of your program. The two layouts perform very similarly, though CardLayout is a little bit more code, though at all not difficult. If you don't want the tab look, which I don't see why you would, for a game, then go with CardLayout
"How this can be achieved using existing JPanels without affecting current functionality? "
You need to create separate JPanel for each containment of whatever components you want in each tab. Then just add those JPanel to the JTabbedPane. It shouldn't break any functionality, just the look. Components from another panel should not be affected, you just won't be able to see any changes made, unless that other panel is in view.
If you want to go with CardLayout you can look at the tutorial

Making multiple frames in a single program

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)

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.

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.

Need to create a WIZARD on Netbeans (jDialog vs CardLayout)?

I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful
Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.
you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.
The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!

Categories

Resources