Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 845
slex Avatar asked Nov 25 '10 17:11

slex


People also ask

What is Swing in GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.


3 Answers

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());         this.panel = new JPanel();         this.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) {         this.panel.add(new JButton("Button"));         this.panel.revalidate();         validate();     }      public static void main(String[] args) {         AddComponentOnJFrameAtRuntime acojfar = new AddComponentOnJFrameAtRuntime();     } } 
  • Resource
like image 60
jmj Avatar answered Sep 21 '22 14:09

jmj


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

screenshot

like image 28
dacwe Avatar answered Sep 21 '22 14:09

dacwe


Component was not visible until setSize() was called:

component.setSize(100,200);
jPanel.add(component);
jPanel.revalidate();
jPanel.repaint(); 
like image 22
Vidura Adikari Avatar answered Sep 20 '22 14:09

Vidura Adikari