Hi all

I'm having trouble using panels in a GUI.
I created panels for steps through a programme. Like when you're installing something, you click "Next" and the GUI switches to the following step.

So for each of these steps, I've created a JPanel. But I was wondering, how can I make the JFrame containing the JPanel switch to another JPanel when the user clicks a button in the current JPanel?

Thx in advance.
Greetings, Kenny.

yourFrame.setContentPane(yourPanel);
yourFrame.repaint();

Or, read the API docs for CardLayout.

Hi masijade

In the meanwhile I found the answer to the problem, but (like usual) now there is another one :icon_wink: .

My main problem was, how can i control the JFrame. Like, when a class extends another one, you can access the superclass with the keyword "super", but I didn't know how to access the class held another class, from that class that is held. Phew, difficult to explain :icon_cheesygrin:

Now I found this solution with the method getParent()
this is code for when a user clicks a button from a panel:

this.setVisible(false);
PanelEvenementenBeheren panelEvenementenBeheren= new PanelEvenementenBeheren();
this.getParent().add(panelEvenementenBeheren);
panelEvenementenBeheren.setVisible(true);
this.getParent().repaint();

/*this.getParent().remove(this);
this.getParent().repaint();*/

also mention the code between comment-signs. The problem now is, when users start working in the program(switching from one panel to another), it keeps on adding panels to the JFrame. So I suppose it's going to get heavier and heavier as users keep working. Maybe even errors because of adding the same panel twice.

But when I try to use the remove() or the removeAll() method for the panel, to remove the current panel, i get NullPointerException's when I try to access the parent again, with the getParent() method.

Greets, K?!

PS: Masijade, when I try to do this.getParent().setContentPane(yourPanel); , well, the parent doesn't seem to have a setContentPane() method :icon_confused:

I didn't know how to access the class held another class, from that class that is held.

That does not make any sense. Try to clarify?

The problem now is, when users start working in the program(switching from one panel to another), it keeps on adding panels to the JFrame. So I suppose it's going to get heavier and heavier as users keep working. Maybe even errors because of adding the same panel twice.

But when I try to use the remove() or the removeAll() method for the panel, to remove the current panel, i get NullPointerException's when I try to access the parent again, with the getParent() method.

The method Masijade suggested to you does not "keep adding panels to the JFrame". It is a set method, so it basically overwrites whatever the previous content pane of the JFrame was when it sets it to the panel that you passed in. But even if it did keep adding windows to the JFrame, a window does not take up much memory, so I doubt there would be errors based on continuously adding windows to the JFrame, unless you did it in an infinite loop or something. I could be wrong about that, but you won't have that problem anyway if you are careful.

You should be using removeAll() on the JFrame, not the JPanel, if you use it at all. But if you use the method Masijade suggested, you do not need to use removeAll at all.

And why are you using getParent, super, or anything to do with the parent again?

Clarification: If a JPanel is held by a JFrame and I want to use a method from the JFrame, but i'm coding in the JPane, then I need to be able to access the JFrame class while coding in the JPanel class.

I know that the method that masijade mentioned does not keep adding panels to the frame, but I'm not able to access this method from within the JPane at this moment.
But I'm still experimenting, maybe something pops up.

Any :icon_idea:ideas?

Greets, K?!

Don't rely on getParent() for getting back to your JFrame. Create a constructor for your panels that takes a reference to that frame as a parameter. All of your panels will then be able to make requests on the main frame. If you create methods such as loadNext() on your frame class then the panels can simply call that and not worry about the mechanics of swapping them.

Yes, someone told me that before, but I wasn't able to make it work at that time.

I got the program to work at the moment (also removing the previous panels), the only thing is that the program always throws a NullPointerException when I click a button (and the panelswitching happens), but it doesn't seem to cause any other problems.

Tanks for the help.
Greetings, Kenny.

Post the relevant code -- the part that is getting the error -- and tell us where the error is occurring. Just paste the error in here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.