OPEN ANOTHER PANEL INSIDE ANOTHER PANEL AFTER PRESSING BUTTON

naomi burke

Total Post:28

Points:196
Posted by  naomi burke
Java 
 436  View(s)
Ratings:
Rate this:
I have been getting an error saying that i didn't add some methods(the action performed) but i already did. I'm having trouble opening the panel2.
public class panel1 extends JPanel implements ActionListener(){
    private panel2 p2=new panel2();
    private JButton button;
    public panel1(){
    button=new JButton("open panel2");
    add(button,BorderLayout.BEFORE_FIRST_LINE);
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            add(p2);
        }
    });
  }
}
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Open another panel inside another panel after pressing button

    You can check the below code for replacing jpanel without switching jframe.  
    contentPanel.removeAll();
            contentPanel.repaint();
            contentPanel.revalidate();
            contentPanel.add(//add your panel here);
            contentPanel.repaint();
            contentPanel.revalidate();

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!