Skip to main content

How to add components dynamically?

8 replies [Last post]
carloshiga
Offline
Joined: 2008-07-04
Points: 0

Hi everybody.

I created a GUI using Netbeans with some JLabels and some JTextFields on a JPanel. Now I need to put a button to add another JTextField, for example, when the users click on it. I've seen that NetBeans created a GroupLayout and added the static components. I tried to do something like this

MyPanel.add(new javax.swing.JTextField("Test"));

but it doesn't work.

Message was edited by: carloshiga

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sourabh8044
Offline
Joined: 2013-05-13
Points: 0

I Know its little bit late to reply, but I guess other people who are looking for similar answers will find it helpful.

Note for NetBeans users, you have to set different layout than NetBeans' default layout (i.e. Free Design) in order to view content on the panel or Frame. To do this, Right Click on Frame/Panel > Select 'Set Layout' > Choose your layout

The code for doing this is:

private void myButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JTextField txt = new JTextField(30);
        JButton bt = new JButton("Check");
 
        jPanel1.add(lbl);
        jPanel1.add(txt);
        jPanel1.revalidate();
        pack();

    }                            

I have tested this code and this runs fine.

s09
Offline
Joined: 2013-06-24
Points: 0

@sourabh8044 Thanks for above solution but it doesn't work for me :(

I am using Netbeans 7.0.1, jdk 1.6

I have created new JFrame, write a method to create check boxes dynamically and call that method inside constructor along with initcomponents(). The code of the method is here:

private void createCheckboxes(){
        //this.setLayout(new java.awt.BorderLayout());
        panel = new javax.swing.JPanel();
        panel.setLayout(new GridBagLayout());
        getRootPane().add(panel);
        for(int i = 0; i<4; i++){
            javax.swing.JCheckBox box = new javax.swing.JCheckBox("check"+i);
            panel.add(box);
            checks.add(box);
            panel.validate();
            panel.repaint();
            pack();
        }
        panel.setVisible(true);
      System.out.print("Worked!!!");
     }

panel and checks are global private variables. I have also tried to set the layout of from in the way you suggested, but nothing is working. The output is just a blank frame, Kindly help me.

Also look here

carloshiga
Offline
Joined: 2008-07-04
Points: 0

I'm trying with MiGLayout now. I created a JPanel that uses MiGLayout. I put a JButton to add components dynamically with an ActionPerformed like this:

MyPanel.add(new javax.swing.JLabel("Name: "));
MyPanel.add(new javax.swing.JTextField(35), "wrap");
MyPanel.validate();
MyPanel.repaint();

The JLabel and the JTextField is added but they do not appear completely! Very weird! I can see only the top part of those components. They appear correctly when I try to resize the window... Is this a bug in MiGLayout?

kirillcool
Offline
Joined: 2004-11-17
Points: 0

Post a complete example at the forums of the specific product:

http://www.miginfocom.com/phpbb3/viewforum.php?f=8

kirillcool
Offline
Joined: 2004-11-17
Points: 0

You'll either need to learn the GroupLayout API or stop relying on drag-n-drop visual editors to do the layout for you. Using grid-oriented third-party layout managers such as FormLayout or MigLayout would be an approach that i'd recommend.

sirbender
Offline
Joined: 2005-04-26
Points: 0

I agree.

I also do not understand why Matisse was created. For Prototyping it is ok, but even for that I do not use it, once you know your way around.

Don't understand me wrong - Matisse is a great piece of programming - but why weren't these resouces spend differently...there are still so many construction sites in Swing and open features in Netbeans :(

sirbender
Offline
Joined: 2005-04-26
Points: 0

Whenever you add a component to a panel you need to call:

panel.validate();
panel.repaint();

carloshiga
Offline
Joined: 2008-07-04
Points: 0

I tried to...

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MyPanel.add(new javax.swing.JButton("test"));
MyPanel.validate();
MyPanel.repaint();
}

but it doesn't work. I think that there's something to do with de automatic layout generated by NetBeans... I don' know..