0

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


    }

}  
M. A. Kishawy
  • 4,943
  • 11
  • 45
  • 72
sumanth
  • 15
  • 7
  • Instead of calling a constructor or setting visibilities, how about creating a panel inside of your buttons action listener? – Smutje Oct 03 '14 at 19:52
  • The timer is never started, so the ActionListener won't be called. Swing components are visible by default, so calling setVisible(true) will have no effect. The JFrame's default layout manager is BorderLayout, meaning that one one component can actively occupy a given position at any one time...creating a new instance of SuperTimer won't do anything as it's never made visible – MadProgrammer Oct 03 '14 at 19:59
  • @Smutje I tried to put the whole code which belongs to the panel which need to be added inside the apction listner of addTimer button but its not creating the panel at all. Please help me i am totally confused – sumanth Oct 03 '14 at 20:11
  • @MadProgrammer please help me out anything else i can do to add the panel dynamically. – sumanth Oct 03 '14 at 20:13
  • 1
    Start with two panels. Add a button to one. Attach an `ActionListener` to the button and when triggered, add a new panel to the first panel (the one without the button). Add the first two buttons to a window. Click madly... – MadProgrammer Oct 03 '14 at 23:16
  • @MadProgrammer Thank you. Do i need to create any seperate lay or is it ok to be with GridBagLayout which is already defined.I think once i add the panel1 to panel2 when an event is triggred i should call rePaint() am i right? – sumanth Oct 04 '14 at 00:14
  • See also this related [example](http://stackoverflow.com/a/7820703/230513). – trashgod Oct 04 '14 at 00:39
  • use the remove and add methods of the Components – BilalDja Oct 04 '14 at 00:42
  • @trashgod Thank you. I got the result but my Jpanel is overlapping the previous Jpanel. Could you please give me an idea how can i overcome this issue. – sumanth Oct 04 '14 at 02:25
  • 1
    Use a `JList` instead. – Andrew Thompson Oct 04 '14 at 03:19
  • @AndrewThompson I did tried to add the panels as the elements of the list but JList is showing strings instead of objects. Is there any way such that i can show my panels inside the list. – sumanth Oct 04 '14 at 11:27
  • *"I did tried to add the panels as the elements of the list"* It is possible to create a `ListCellRenderer` that uses the panel as the rendering component. But it is probably better to put the 'object' that the panel displays into the list, then define a `ListCellRenderer` that returns the panel. So, what exactly is in these panels? – Andrew Thompson Oct 04 '14 at 14:30
  • @sumanth: `JList` is more flexible and scalable, but adapting the [example](http://stackoverflow.com/a/7820703/230513) may be a useful exercise. Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your chosen approach. – trashgod Oct 04 '14 at 14:41
  • @trashgod Thank you. My problem is solved i used BoxLayout at frame level and used GridbagLayout at panel level. Thnaks all for your support. – sumanth Oct 04 '14 at 18:55
  • You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod Oct 04 '14 at 20:11

0 Answers0