Giannis
Giannis

Reputation: 5526

Changing content of a Frame

I am making a GUI for a Java application. What I am trying to do is have a frame, and based on user actions change the panel being displayed. Although if I do something like this :

private void setFrameContent(Container content) {
        appFrame.setContentPane(content);
    }

the frame will just freeze once I try to change the JPanel being displayed. Also tried some variations on this.

How should I change the content of the frame?

One option would be CardLayout but that would mean I would have to create all the panels on app startup while some of them might not be used. What other ways are there to achieve this?

Upvotes: 1

Views: 149

Answers (1)

mKorbel
mKorbel

Reputation: 109813

1) if you add JComponent to the already visible Container, then you have to call for revalidate() and repaint(), in your case

  • for Java7 is there implemeted revalidate() and repaint() to JFrame directly

  • for Jav6 and lower version you have to revalidate() and repaint() for ContentPane

2) I think that CardLayout (still) could be better options for switching betweens Views in Swing GUI

Upvotes: 2

Related Questions