2010-11-25 3 views
22

Was ich versuche zu tun ist ein ähnliches Prinzip wie das Hinzufügen von Anhängen zu E-Mails, Sie können auf eine Schaltfläche klicken und ein neues Durchsuchen-Feld würde sich öffnen und die Anzahl der separaten Anhänge erhöhen, die Sie haben können.java - Wie würde ich die Swing-Komponente dynamisch zum Klick hinzufügen?

Ich bin ziemlich neu also wenn mich jemand auf ein Beispiel hinweisen könnte?

+0

hinzufügen Sie können es tun, wie Sie es statisch zu tun, aber sie können schönere Lösungen sein, je nachdem, was Sie tun wollen. – khachik

Antwort

31

Beispielcode Buttons on the fly dynamisch hinzuzufügen.

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

Voll 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(); 
    } 
} 
+0

danke ...! es ist sehr hilfreich ... hehehe .... – gumuruh

+6

Soweit ich weiß, sollte 'revalidate()' und/oder 'validate()' gefolgt von 'repaint()' (oder Änderungen werden nicht referenziert werden) auch mit ' validate() 'ist überflüssig, da' revalidate() '' 'validate()' –

+0

thanks..it aufruft –

9
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

0

Komponente war nicht sichtbar, bis setSize() genannt wurde:

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

Pfanne el.add (Knopf);

panel.revalidate();

panel.repaint();

0

Java: Dynamisch Swing Komponenten

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 
Verwandte Themen