Search code examples
javaswingappletjcomponent

Add JComponent to applet's pane


I am trying to add an JComponent (label, for instance) to the applet pane when the button is pressed. I have the following piece of code:

public class TestApplet extends JApplet
{
    @Override
    public void init()
    {
        setLayout(new FlowLayout());
        JButton bt = new JButton("hit it");
        bt.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                // getContentPane().setFont(null);
                getContentPane().add(new JLabel("to the right"));
            }
        });
        add(bt);
    }
}

This does not make the label visible, unless I uncomment getContentPane().setFont(null);

Please advise how should I display the label properly. Thanks in advance.


Solution

  • getContentPane().add(new JLabel("to the right"));
    this.revalidate();