Erase Swing title bar / panel and display a new panel

I created an applet where, when you click the "Forgot Password" button, I delete the current JPanel on the applet and create a new JPanel that displays the JComponents associated with receiving / forgetting the password.

I can successfully clear JPanel using .removeAll (); BUT, after I create all my new JComponents and add them to the content panel (main JPanel), the applet just saddles and does not display the new JPanel and UNLESS components . resize the applet, then it repaints and works.

I tried putting .invalidate () after I created all new JComponents, but which still does not update the applet?

How can I create my JPanel after cleaning it with .removeAll () and add different JComponents to it?

The 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
    }
}
+3
source share
2 answers

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

+5
source

Another “clean” option is to replace the views with CardLayout. He does all the dirty work backstage.

0
source

Source: https://habr.com/ru/post/1790844/


All Articles