How to use CardLayout with JPanels being created in different classes? - java

I am trying to create a simple Java Swing GUI application. I basically want a main menu with buttons that will display the rules and the games. Say I have 3 classes: Menu, Game, and Rules. Now, I want to create a JPanel inside each class for that specific GUI. I am having trouble figuring out how to use CardLayout to switch between the JPanels using the same JFrame. Does someone have a clear explanation for this? Should I create another class for the CardLayout? Thanks

Related

Adding new objects to a jframe

I have seen a program and was hoping to replicate one feature of it in my program. The program is a flashcard app, and when you create a new deck it adds another section to the homescreeen. I will attach images. I would love some advice on how to do this.
One of possible way is to use correct layouts, here is examples. Using this you may dynamically add any components (labels, images, buttons etc.) into your JPanel (I recommend to use JPanel as main container of all components instead of JFrame that should be one and just contain your JPanel).

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 do I create a JPanel and put it on a JFrame from another class?

I am trying to learn some Java GUI stuff. I am trying to create a special jPanel, which I have created in another class [it is a Panel with two labels on it] inside one class, and using a passed JFrame add it to that JFrame from the other class. I have been trying myJFrame.add(thePanel) and such, but it never seems to actually do anything. I have tried doubly making sure it is sset to visible, is at a reasonable location, ect.
I am trying to create the panel in one class, and .add it to the given jframe. There is probably something I am not understanding.
I am not the best at the java GUI stuff... so please be kind :) Any advice you can give would be wonderful.
I am using Netbeans. If I drag the special Panel I created over to my panel in the Design editor, it adds an instance of that panel, but I can't seem to make my own instance of this and have it appear.

Want a class to run on a JPanel when I click a button

I'm using NetBeans design mode to make a basic GUI. I have layered panels of equal size in the center of the app with buttons on either side These buttons setvisible(false) all panels and setvisible(true) the panel corresponding to the button (i.e. clicking the "team" button turns off all panels and turns on "team" panel etc).
My issue is that I want to instantiate a class on to each panel so as I can use inheritance to share information and use I/O as well as displaying any information.
Its a World Cup app where there are panels for each category, like teams, players, stadiums, fixtures etc. I'm learning how to use inheritance and want to be able to inherit players into teams and a few other things. Thanks in advance to anyone with suggestions.
So I'm using NetBeans design mode to make a basic GUI.
My first suggestion would be don't use GUI builders. Start reading Swing tutorials to learn how this API works and code your GUI classes by hand. It's fundamental learn about Layout Managers.
These buttons setvisible(false) all panels and setvisible(true) the
panel corresponding to the button (i.e. clicking the "team" button
turns off all panels and turns on "team" panel etc).
Switching panels can be easily done using CardLayout. See How to Use CardLayout tutorial.
I'm learning how to use inheritance and want to be able to inherit
players into teams and a few other things
Inheritance establishes an "is-a" relationship between two classes. Rarely a Player can be considered as a Team. Maybe you want to do something like this:
class Team {
private List<Player> players;
public void addPlayer(Player player) {...}
public void removePlayer(Player player) {...};
public List<Player> getPlayers() {...};
}
But this is not inheritance at all. Start reading Inheritance article

Creating a GUI with multiple panels and one frame

I am trying to create a simple GUI. I have a menu bar that is filled with various JMenuItems. Each menu item should link to a different "window". Currently, I am thinking the best way to do this is to create a single frame, and create various JPanels. My ActionListeners will toggle visibility of the different panels, and only one panel should be visible at a time. Is this the best way to go about the task? Or is there a better workaround.
Yes, the best way is to use a CardLayout of which there's a great tutorial (please see the link), and many examples online including in this very forum, several of which I've written, including:
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java
JTabbedPane is already implemented for you!
JTabbedPane's tutorial.

Categories

Resources