-1

I have an application that has a BorderLayout. In the Center of this layout I have a JPanel that is called mainContent.

Now I have a method like this:

public void setMainPanel(JPanel panel){
    mainPanel = panel;
    gui.repaint();
    gui.revalidate();
}

Here I can set the reference from the mainPanel to a new panel, but it's not possible to make it visible! So I have no idea how I can refresh the GUI!

I tried repaint and revalidate! The variable GUI is the JFrame! The setMainPanel method is defined in the frame Class.

How can I refresh/repaint the content?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lukas Hieronimus Adler
  • 1,063
  • 1
  • 17
  • 44
  • 4
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jan 27 '15 at 12:37

1 Answers1

3

This line doesn't do what you think it does:

mainPanel = panel;

This just updates the reference of mainPanel variable to point to panel, but it won't update the container with BorderLayout, because this container will keep a reference to the previous panel.

Assuming gui is the container with BorderLayout, you should do as follows:

public void setMainPanel(JPanel panel){
    gui.add(panel, BorderLayout.CENTER);
    gui.revalidate();
    gui.repaint();
}

Off-topic

Please take a look to this topic: Is Java “pass-by-reference” or “pass-by-value”?

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69