Removing JPanel from a JPanel - java

I have a State Manager for a game that contains a stack of States. Now in one of my states ("Menu"), I have two JButtons in a JPanel (the JPanel is added to the main JPanel that displays everything). When I click the JButton "Play", I remove the Menu state from the stack. However, the JButtons stay on the screen (even though when i peek() I see that the Menu has been removed from the stack).
I don't want to remove the buttons from the State, because I want to be able to go back to Menu and see my buttons there again. How can I remove the buttons along with the state?

Have you considered using a CardLayout?
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.

Assuming each state corresponds to a single JComponent, you can use the .setVisible(false); method to hide the item when the state is removed.
In this case, when you initialize the main component, you'd want to make sure all components have been added - then when you add/remove states, you can just toggle the visibility.

Related

Java and working with different instances

I have a program where I can click on a button and it calls in a new panel, making the current one false through action listener. This panel uses up the whole size of the JFrame, giving me a form to fill out and buttons to press.
Is this the correct method to do this? Or is there a better way as I am running into bugs where the newly called JPanel appears on other JPanels. I could use setVisible(false) but then certain elements such as buttons do not appear.
According to what you want, it appears to me you would probably want to look at CardLayout where you can swtich 2 or more panels in the frame.
Conceptually, each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:
By asking for either the first or last card, in the order it was added to the container
By flipping through the deck backwards or forwards
By specifying a card with a specific name

Bringing component on JLayeredPane to the front when their listener performs an action

I have a JLayeredPane and a couple of separate objects that extend JLabel, which are added to the pane. Said objects have a MouseListener added to them and now I want to bring the objects to the front, whenever their listeners performs their action.
I had the idea of referring to the JLayeredPane from inside of the added object, but i can't seem to access it from there.
So how would i get the (clicked on/ dragged/ whatever) object infront of the others?

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)

CardLayout - How to use previous() and next() using NetBeans GUI Builder?

Here is what I tried:
Dragged some JPanels onto a JFrame (using NetBeans inspector window).
In JFrame constructor, made all JPanels invisible using .setVisible(false), except the one I want to show first.
It works and I can easily go from one to another by using some buttons with actionPerformed and adding .setVisible(false) to the current card and .setVisible(true) to the one I want to see.
What I wanted to do now is to use CardLayout previous() and next(), similar to a browser's back/forward. I also would like to reach to a panel from different places, i.e., two panels can link to the same one, so previous panel wouldn't always be the same.
I tried using the following code in an actionPerformed inside JFrame class:
CardLayout cardLayout = (CardLayout) this.getLayout();
cardLayout.previous(this);
However, it doesn't work. What am I missing? Is this supposed to do what I'm looking for?
As you have set the layout of your JFrame to CardLayout, you will need to use the parent container when using its next() & previous() methods. For JFrame the parent container is the content pane. So change:
cardLayout.previous(this);
to
cardLayout.previous(getContentPane());
Declare a variable String previousCard in your JPanel. When you go from CardA to CardB set previousCard variable to "CardA" or whatever the card's name is. So after setting this for all transitions from one card to other, the back buttons will always do the same thing.
cardLayouot.show(getContentPane(), previousCard);

Assigning a different (new) JPanel to a JTabbedPane at run time

I have a JTabbedPane (say myTabPane) having one tab (lets take only one tab for clarity sake). While creating the JTabbedPane, I added a JPanel (say panel_A) to this tab. I have a button on this JPanel. The tab displays my JPanel perfectly with the button on it. So far so good.
I have defined a listener on the button which creates an instance (say panel_B) of another class extending JPanel. This JPanel has got a different set of components on it. I want panel_B to super-impose panel_A. That is, JTabbedPane's tab should show panel_B and hide panel_A.
Please note that I am able to display panel_A OR panel_B when I "bind" the respective panel (one of them) to the tab during creation of the JTabbedPane. However, I want a selective display (or binding, whichever is possible) of only one of the panels with a button-click (ie. at runtime).
How can this be achieved?
Thank you!
This will do what you need:
myTabPane.removeTabAt(0);
myTabPane.addTab("B", panelB);

Categories

Resources