how can i add components dynamically in a jpanel? I am having add button when i click the button the components should be added to the JPanel.
my question is that ad
Use an ActionListener
, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.