Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase Swing Content Pane/Panel & display a new panel

Tags:

java

swing

I have created an applet where when you press the button "Forgot Pass" I erase the current JPanel on the applet & create a new JPanel that displays the JComponents related to Retrieving/Forgetting a password.

I can successfully clear the JPanel using .removeAll(); BUT after I create all my new JComponents & add them to the content pane (main JPanel) the applet just goes gray & doesn't show the new JPanel & components UNLESS I resize the applet, then it repaints & works.

I have tried putting .invalidate() after I have created all the new JComponents but that still doesn't refresh the applet?

How can I make my JPanel appear after I clear it with .removeAll() & add different JComponents to it?

Code:

public class App extends JApplet
{
    JPanel mainPanel; 

    public void init()
    {
        SwingUtilities.invokeAndWait( new Runnable() {
            public void run()
            {
                showLoginPanel(); // this shows fine on loading
            }
        });

    }

    public void showForgotPassPanel()
    {
        mainPanel.removeAll();

        mainPanel = (JPanel) getContentPane();
        Box hBox  = Box.createHorizontalBox();
        Box vBox  = Box.createVerticalBox();
        mainPanel.setLayout( new BorderLayout() ); 

        ... create components

        ... add components to mainPanel

        mainPanel.invalidate(); // doesn't make new layout visible, not unless I resize the applet
    }
}
like image 516
Jim Avatar asked Feb 08 '11 23:02

Jim


1 Answers

Use mainPanel.revalidate(); and/or mainPanel.repaint(); methods.

like image 176
POSIX_ME_HARDER Avatar answered Sep 28 '22 04:09

POSIX_ME_HARDER