I am building a GUI application where it should add the panels dynamically into a single frame when an event is generated by clicking on JButton. There should not be any limitation to add the panels if we push the button "n" times it should add the same panel "n" times. Tried to run the same code again by calling the constructor inside the event method of the butoon but panel is adding only once. Please help me out i did a lot of search regarding this but unable to come out with a proper solution.
As in the below code i want to add the panel when the button from panel1 is pushed dynamically "n" number of times.
Thanks in advance.
*
public class SuperTimer extends JFrame
{
JButton addTimer;
JPanel panel;
public SuperTimer()
{
panel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx=0.5;
c.weighty=0.5;
c.gridx=1;
c.gridy =1;
c.insets= new Insets(10, 10, 10, 10);
addTimer = new JButton("Add Timer");
panel1.add(addTimer,c);
AddTimer at = new AddTimer();
addTimer.addActionListener(at);
getContentPane().add(panel1,BorderLayout.NORTH);
getContentPane().add(panel,BorderLayout.CENTER);
}
public class AddTimer implements ActionListener
{
public void actionPerformed(ActionEvent h)
{
SuperTimer kt = new SuperTimer();
panel.setVisible(true);
}
}
}
*
*
public class Trigger {
public static void main(String[] args) {
SuperTimer st = new SuperTimer();
st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
st.setSize(500,500);
st.setVisible(true);
}
}