I am trying to make a popup panel which is activated with the help of JToggleButton. I want the JPanel to be added onto the other Jpanel when ToggleButton is selected and hides
The problem is you keep creating instances of your JPanel.
You could remove the JPanel if your JToggleButton is not selected and add an already created instance of your JPanel if the button is selected.
See this simple example:
public class MainFrame extends JFrame {
private JPanel topPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JToggleButton toggleButton = new JToggleButton("Toggle");
public MainFrame() {
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.topPanel.setPreferredSize(new Dimension(100, 100));
this.centerPanel.setPreferredSize(new Dimension(100, 100));
this.toggleButton.setPreferredSize(new Dimension(100, 100));
this.add(topPanel, BorderLayout.NORTH);
this.add(centerPanel, BorderLayout.CENTER);
this.add(toggleButton, BorderLayout.SOUTH);
this.toggleButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
add(centerPanel, BorderLayout.CENTER);
} else {
remove(centerPanel);
}
pack();
}
});
this.pack();
this.setVisible(true);
}
}
You can see that centerPanel is only instantiated once.