我试图拥有两个Jcombox,其中第二个Jcombox应该根据第一个Jcombox的更改来更改其值。
我尝试过但未能成功,不胜感激。谢谢
到目前为止,这是我尝试过的:
public class SharedDataBetweenComboBoxSample {
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
public static void main(String args[]) {
final String labels[] = { "A", "B", "C" };
final String labelsA[] = { "A", "AA", "AAA" };
final String labelsB[] = { "B", "BB", "BBB" };
final String labelsC[] = { "C", "CC", "CCC" };
final JFrame frame = new JFrame("Shared Data");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem(labels);
comboBox1.setSelectedItem(null);
final JComboBox comboBox2 = new JComboBox();
// comboBox2.setEditable(true);
panel.add(comboBox1);
panel.add(comboBox2);
frame.add(panel,BorderLayout.NORTH);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
System.out.println("Item: " + itemEvent.getItem());
ItemSelectable is = itemEvent.getItemSelectable();
System.out.println(", Selected: " + selectedString(is));
if (selectedString(is) == "B") {
comboBox2.addItem(labelsB);
// frame.add(comboBox1, BorderLayout.CENTER);
} else if (selectedString(is) == "A") {
comboBox2.addItem(labelsA);
// frame.add(comboBox1, BorderLayout.CENTER);
} else if (selectedString(is) == "C") {
comboBox2.addItem(labelsC);
// frame.add(comboBox1, BorderLayout.CENTER);
} else {
comboBox2.setSelectedItem(null);
// frame.add(comboBox1, BorderLayout.CENTER);
}
}
};
comboBox1.addItemListener(itemListener);
frame.setSize(300,200);
frame.setVisible(true);
}
}
最佳答案
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ComboBoxTwo extends JFrame implements ActionListener
{
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable subItems = new Hashtable();
public ComboBoxTwo()
{
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
mainComboBox = new JComboBox( items );
mainComboBox.addActionListener( this );
getContentPane().add( mainComboBox, BorderLayout.WEST );
// Create sub combo box with multiple models
subComboBox = new JComboBox();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
getContentPane().add( subComboBox, BorderLayout.EAST );
String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
subItems.put(items[1], subItems1);
String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
subItems.put(items[2], subItems2);
String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
subItems.put(items[3], subItems3);
}
public void actionPerformed(ActionEvent e)
{
String item = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( item );
if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}
public static void main(String[] args)
{
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}