how to achieve an unlimited number of JCombolBoxes

Hi. I want to ask you guys of how to achieve an unlimited number of JCombolBoxes.

What I want my program do is tht.

First, when the program starts, only one JConbolBox(1) shows up.

When user clicks on that JCombolBox(1), the other JcombolBox(2) shows up right underneath it.

When user clicks on that JCombolBox(2), the other JcombolBox(3) shows up right underneath it.

...

So there might be ended up with 10, 100 or even more JCombolBoxes on the Jpanel. (no limit)

The question is, how should I initialize my JcombolBoxes.

Obviously, if i do:

jComboBox1 = new JComboBox();

jComboBox2 = new JComboBox();

all the way to jComboBox1000 = new JComboBox(); is not going to work out.

If I do it in a loop, i can just stick with jComboBox1 = new JComboBox(); and place it in different locations. However, I dont think i can use actionlistener or getselected method for it.

So what is ur solution.

[974 byte] By [Shaodw_boia] at [2007-11-27 11:21:16]
# 1

the most obvious question that pops up in my mind is "why"?

What is your motivation for doing something like this?

petes1234a at 2007-7-29 14:47:41 > top of Java-index,Java Essentials,Java Programming...
# 2

I agree with petes1234 but here's an examplepublic class Funny {

private static String[] comboValues = new String[] {"One", "Two"};

private static void initAndShowGUI() {

JFrame frame = new JFrame("Funny combos");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel comboPane = new JPanel();

BoxLayout layout = new BoxLayout(comboPane, BoxLayout.Y_AXIS);

comboPane.setLayout(layout);

JComboBox combo = new JComboBox(comboValues);

combo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox source = (JComboBox)e.getSource();

String selected = (String)source.getSelectedItem();

if ("Two".equals(selected)) {

source.removeActionListener(this);

JComboBox newCombo = new JComboBox(comboValues);

newCombo.addActionListener(this);

comboPane.add(newCombo);

comboPane.revalidate();

}

}

});

comboPane.add(combo);

JPanel contentPane = new JPanel(new BorderLayout());

contentPane.add(comboPane, BorderLayout.NORTH);

frame.setContentPane(contentPane);

frame.setSize(640, 480);

frame.setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

initAndShowGUI();

}

});

}

}

dwga at 2007-7-29 14:47:41 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks dwg. I think you codes are pretty much of what i want.

But i think i should delete this line : source.removeActionListener(this);

cuz i allow the user to change s/he mind and to another value from the jombolboxes.

But a question arises.

How do I get a list of selected values from all the Jcombolboxes on this panel (into array maybe) at the end of the program (like, when the user clicks save, i need to save all the selective values into my .txt file)

Since many combolboxes are declared and constructed on run time. So i dont know how to do getSelected() for each of them.

public class Funny {

private static String[] comboValues = new String[] {"Book1", "Book2", "Book3", "Book4", "Book5"};

private static void initAndShowGUI() {

JFrame frame = new JFrame("Funny combos");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel comboPane = new JPanel();

BoxLayout layout = new BoxLayout(comboPane, BoxLayout.Y_AXIS);

comboPane.setLayout(layout);

JComboBox combo = new JComboBox(comboValues);

combo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox source = (JComboBox)e.getSource();

String selected = (String)source.getSelectedItem();

JComboBox newCombo = new JComboBox(comboValues);

newCombo.addActionListener(this);

comboPane.add(newCombo);

comboPane.revalidate();

}

}

});

comboPane.add(combo);

JPanel contentPane = new JPanel(new BorderLayout());

contentPane.add(comboPane, BorderLayout.NORTH);

frame.setContentPane(contentPane);

frame.setSize(640, 480);

frame.setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

initAndShowGUI();

}

});

}

}

Shaodw_boia at 2007-7-29 14:47:41 > top of Java-index,Java Essentials,Java Programming...
# 4

In the future, Swing related questions should be posted in the Swing forum.

You added the combo boxes to the panel. So take a look at the Container API. You can iterate through all the components added to the panel and get the values.

camickra at 2007-7-29 14:47:41 > top of Java-index,Java Essentials,Java Programming...