How can you reset the contents of a JComboBox?
I'm still new at Java programming, and I'm trying to create a program that has a main JComboBox, which when an item in it is selected, the contents of 2 sub combo boxes change. Here is what I've done so far:
String[] main ={...};
String[] acc ={...};
String[] ang ={...};
JComboBox unitChooser =new JComboBox(main);
JComboBox inputChooser =new JComboBox(acc);
JComboBox outputChooser =new JComboBox(acc);
//unitChoser's listener
unitChooser.addActionListener
(
new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
JComboBox temp = (JComboBox)e.getSource();
item = temp.getSelectedIndex();
changeSubs(item);
}
}
);
//inputChooser's listener
inputChooser.addActionListener
(
new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
JComboBox temp = (JComboBox)e.getSource();
itemIn = temp.getSelectedIndex();
}
}
);
//outputChooser's listener
outputChooser.addActionListener
(
new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
JComboBox temp = (JComboBox)e.getSource();
itemOut = temp.getSelectedIndex();
}
}
);
//Change the contents of the two sub combo menus
publicstaticvoid changeSubs(int index)
{
switch (index)
{
case 0:
{
inputChooser =new JComboBox(acc);//Shouldn't this reset it?
outputChooser =new JComboBox(acc);
break;
}
case 1:
{
inputChooser =new JComboBox(ang);
outputChooser =new JComboBox(ang);
break;
}
}
}
When I worked with the graphics paint() function to draw 2-D images, the Color class parameters could be reset in this same way. I checked the API but there's no specific command for resetting the contents contained in a String[] array. The compiler accepts the commands but nothing happens when it's executed. Any help would be greatly appreciated!
[3785 byte] By [
Jason102a] at [2007-10-2 10:42:57]

to reset them, you change the model
here's an example of 3 comboboxes
selections in cbo1 changes cbo2 and cbo3,
selections in cbo2 changes cbo3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
{
String[] cbo1Text = {"Snacks","Drinks","Cigarettes"};
String[][] cbo2Text = {{"Chocolate Bar","Chips","Candy"},{"Soft","Beer","Wine","Spirits"},
{"Menthol","Camel","Marlboro","Lite"}};
String[][][] cbo3Text = {{{"Choc1","Choc2","Choc3"},{"Chips1","Chips2","Chips3"},{"Candy1","Candy2","Candy3"}},
{{"Coca Cola","Pepsi","Dr Pepper","Solo"},{"Bud","Coors","Heinekken"},
{"Champagne","Chardonnay","Shiraz"},{"Bourbon","Scotch","Vodka"}},
{{"Menthol15","Menthol20","Menthol40"},{"Camel15","Camel20","Camel40"},
{"Marlboro15","Marlboro20","Marlboro40"},{"Lite15","Lite20","Lite40"}}};
JComboBox cbo1 = new JComboBox(cbo1Text);
JComboBox cbo2 = new JComboBox(cbo2Text[0]);
JComboBox cbo3 = new JComboBox(cbo3Text[0][0]);
public Testing()
{
super("JComboBox Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(400,300);
setSize(300,75);
getContentPane().setLayout(new FlowLayout());
cbo1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DefaultComboBoxModel cbo2NewModel = new DefaultComboBoxModel(cbo2Text[cbo1.getSelectedIndex()]);
cbo2.setModel(cbo2NewModel);
DefaultComboBoxModel cbo3NewModel = new DefaultComboBoxModel(cbo3Text[cbo1.getSelectedIndex()][0]);
cbo3.setModel(cbo3NewModel);}});
cbo2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DefaultComboBoxModel cbo3NewModel = new DefaultComboBoxModel(cbo3Text[cbo1.getSelectedIndex()][cbo2.getSelectedIndex()]);
cbo3.setModel(cbo3NewModel);}});
getContentPane().add(cbo1);
getContentPane().add(cbo2);
getContentPane().add(cbo3);
}
public static void main(String[] args){new Testing().setVisible(true);}
}