I've look (almost) everywhere(here and here specifically) to find an answer on my problem. I'm trying to add an unknown number of JButton
to a JScrollPane
depending of the selection of a JComboBox
. What I know is you have to add the element to a JPanel
in order to get them in your GUI.
Here's an example code : (It does'nt work), it is an example)
public class test {
ArrayList<JButton> button = new ArrayList<JButton>();
String[] stringArray = {"Goat", "Cow", "Dog", "Cat", "Human"};
JComboBox comboBox = new JComboBox(stringArray);
JPanel containerPanel = new JPanel();
JScrollPane scroller = new JScrollPane(containerPanel);
public void addButton(String btnName) {
button.add(new JButton(btnName));
containerPanel.add(button.get(button.size() - 1));
}
public class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
addButton(comboBox.getSelectedItem().toString());
}
}
}
I want to add as many JButton
as the user want.
If i just add them like this :
containerPanel.add(new JButton("test"));
It works, but that's not what i want to achieve.
Can you help me?
EDIT: ___________________________________________________________________________
Here is some code that compile and is a simplified replica of what i'm trying to do :
public class Frame extends JFrame {
String[] list = {"Human", "Goat", "Dog", "Cat", "Duck"};
ArrayList<JButton> button = new ArrayList<JButton>();
JComboBox cBox = new JComboBox(list);
JPanel container = new JPanel();
JScrollPane scroller = new JScrollPane(container);
public Frame() {
cBox.addActionListener(new Action());
this.setLayout(new BorderLayout());
this.setSize(200, 200);
this.add(cBox, BorderLayout.SOUTH);
this.add(scroller, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void createBtn(String s) {
System.out.println("Button's label : " + s);
button.add(new JButton(s));
System.out.println("Button's index : " + (button.size() - 1));
container.add(button.get(button.size() - 1));
}
public class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action made");
JComboBox temp = (JComboBox) e.getSource();
createBtn(temp.getSelectedItem().toString());
}
}
}