Home > DeveloperSection > Forums > Open another panel inside another panel after pressing button
naomi burke
naomi burke

Total Post:28

Points:196
Posted on    December-04-2015 11:28 PM

 Java Java 
Ratings:


 1 Reply(s)
 163  View(s)
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);
        }
    });
  }
}


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    December-04-2015 11:36 PM

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();

Don't want to miss updates? Please click the below button!

Follow MindStick