1

I am adding the components dynamically at runtime on clicking the button. But now i want to add components Dynamically without clicking button. How can i do that..?? Here is my source code for adding a component on clicking the Button.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String[] items = new String[4];
        items[0]="English";
        items[1]="French";
        items[2]="Spanish";
        items[3]="Hindi";
        JTextField jtextfield = new JTextField();
        jtextfield.setPreferredSize(new Dimension(50, 20));
        JButton jbutton = new JButton();
        jbutton.setText("Remove");
        jbutton.setPreferredSize(new Dimension(89, 23));

        JLabel jlabel = new JLabel();
        jlabel.setText("Text:");
        jlabel.setPreferredSize(new Dimension(40, 20));
        JLabel jlabel2 = new JLabel();
        jlabel2.setText("Language:");
        jlabel2.setPreferredSize(new Dimension(65, 20));

        JComboBox jcombo = new JComboBox();
        jcombo.setPreferredSize(new Dimension(80,20));
        jcombo.addItem(items[0]);
        jcombo.addItem(items[1]);
        jcombo.addItem(items[2]);
        jcombo.addItem(items[3]);

        jPanel6.add(jlabel);
        jPanel6.add(jtextfield);
        jPanel6.add(jlabel2);
        jPanel6.add(jcombo);
        jPanel6.add(jbutton);

        jbutton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Component[]storeAllButtonInPanel = jPanel6.getComponents();

                if(storeAllButtonInPanel.length!=0) {
                    jPanel6.remove(storeAllButtonInPanel.length-1);
                    jPanel6.remove(storeAllButtonInPanel.length-2);
                    jPanel6.remove(storeAllButtonInPanel.length-3);
                    jPanel6.remove(storeAllButtonInPanel.length-4);
                    jPanel6.remove(storeAllButtonInPanel.length-5);

                    jPanel6.revalidate();
                    validate();
                    repaint();
                }
            }

        });



        jPanel6.validate();
        jPanel6.repaint();
    }

And if i have only 2 values of Text then it also disply two rows and if 3 values then there should be only 3 rows..!! How can i do that.?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
ManthanB
  • 404
  • 2
  • 5
  • 21
  • You might want to write code in the "Constructor" method. – KV Prajapati Sep 06 '11 at 07:07
  • 1
    unrelated to your problem (just glaring at me in your example :) Do not use setXXSize (XX == min/pref/max) for reasons, see http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing – kleopatra Sep 06 '11 at 09:39

2 Answers2

2

no idea what do you want, there is needed / required some control about that, as output to the GUI and by using some of Listener (as your ActionListener),

maybe (with full control) JPopupMenu, API, examples here or here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

An instance of javax.swing.Timer can periodically invoke the actionPerformed() method of an ActionListener, as suggested in this example that adds and removes JLabels.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045