java – How would I dynamically add swing component to gui on click?

What I am looking to do is a similar principle to adding attachments to emails, you can click a button and a new browse box would open increasing the number of separate attachments you can have.

I’m fairly new so if someone could point me towards an example?

how to dynamically add swing components to gui on click and make it permanent

This question might be relevant to this: java – How would I dynamically add swing component to gui on click? But would it be possible to make the changes you did during runtime be permanent and see it

java swing dynamically adding components [duplicate]

This question already has an answer here: java – How would I dynamically add swing component to gui on click? 2 answers I am new to Java Swing. I have some doubt regarding adiing components dyn

Java add buttons dynamicaly as an array [duplicate]

Possible Duplicate: java – How would I dynamically add swing component to gui on click? I want to add array of buttons dynamically. I tried like this: this.pack(); Panel panel = new Panel(); panel.s

Java Swing: How to change GUI dynamically

I need to add components dynamically. Moreover, I need to alter the layout dynamically.

Dynamically adding toolbars in Java Swing GUI

I am developing a Java Desktop Application. In the GUI, I want that user can add as many toolbars dynamically as he wants. To implement this, the following are the things that I have done already: Ha

Dynamically create Swing GUI from generic XML

I would like to dynamically create a Java Swing GUI from generic XML, and have the GUI output XML. Can anyone suggest a framework please? I did try JAXFront, it would potentially fulfill my requiremen

Handling Click for A Custom Swing component Extending JPanel?

How would you handle click in a Custom swing Component that is extending JPanel. I am making a game and I am wondering how i can handle the clicks. Specifically in a different thread than the gui, and

How can I “pack()” a printable Java Swing component?

I have implemented a Java Swing component that implements Printable. If I add the component to a JFrame, and do this.pack(); on the JFrame, it prints perfect. But if I don’t add the component to a JFr

How do I control Java/Swing GUI component alignment using NetBeans IDE?

As a NetBeans novice, I’m laying out a pretty simple 2-panel GUI using NetBeans 7.0, and I’m having the craziest time getting some components (JButtons, JLabels, JSlider) to stay where I position them

hex viewer/editor GUI component for Java Swing?

I’m looking for a decent hex viewer (read-only; I don’t need an editor) GUI component that I can use within my Swing GUI. Is there anything out there? I suppose I could write my own, but am hoping to

Answers

public static void main(String[] args) {

    final JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(0, 1));

    frame.add(new JButton(new AbstractAction("Click to add") {
        @Override
        public void actionPerformed(ActionEvent e) {

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    frame.add(new JLabel("Bla"));
                    frame.validate();
                    frame.repaint();
                }
            });
        }
    }));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

java - How would I dynamically add swing component to gui on click?

Sample code to add Buttons on the fly dynamically.

panel.add(new JButton("Button")); 
validate(); 

Full code :

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class AddComponentOnJFrameAtRuntime extends JFrame implements ActionListener
{
JPanel panel;

public AddComponentOnJFrameAtRuntime()
{
 super("Add component on JFrame at runtime");
 setLayout(new BorderLayout());
 panel=new JPanel();
 panel.setLayout(new FlowLayout());
 add(panel,BorderLayout.CENTER);
 JButton button=new JButton("CLICK HERE");
 add(button,BorderLayout.SOUTH);
 button.addActionListener(this);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setSize(500,500);
 setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
 panel.add(new JButton("Button"));
 panel.revalidate();
 validate();
}

public static void main(String[]args)
{
 AddComponentOnJFrameAtRuntime acojfar=new AddComponentOnJFrameAtRuntime();
}
}

Component was not visible until setSize() was called:

component.setSize(100,200);
jPanel.add(component);
jPanel.revalidate();
jPanel.repaint(); 

panel.add(button);

panel.revalidate();

panel.repaint();

Java : Dynamically add swing components

for Example : count=3
//Java Swing: Add Component above method
public void  dya_addcomp(int count)
{
//Dynamicaly Delete Image_icon
 BufferedImage Drop_Tablefield = null;
 try {
     Drop_Tablefield = ImageIO.read(this.getClass().getResource("/images/drop.png"));
 } catch (IOException ex) {
     msg(" Error: drop and edit icon on Table, "+ex);
 }
 //count Items:  3 times for loop executed..
 for(int i=0;i<count;i++)
 {
     //cnt++;
     //lblcount.setText("Count : "+cnt);
     JTextField txtcolnm=new JTextField("",20);
     JComboBox cmbtype=new JComboBox();
     JTextField txtcolsize=new JTextField("",20);

     JButton Drop_Table_Field = new JButton(new ImageIcon(Drop_Tablefield));

     cmbtype.addItem("INTEGER"); cmbtype.addItem("FLOAT");
     cmbtype.addItem("STRING");  cmbtype.addItem("BOOLEAN");

     colnamepanel.add(txtcolnm);   colnamepanel.add(cmbtype);
     colnamepanel.add(txtcolsize); colnamepanel.add(Drop_Table_Field);

     colnamepanel.setAutoscrolls(true);

     //refresh panel
     colnamepanel.revalidate();
     colnamepanel.repaint();

     //set the layout on Jpanel
     colnamepanel.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
  }//end for loop
 }//end method