2010-11-04 77 views
0

我试图有两个Jcombox,其中第二个Jcombox应根据第一个Jcombox中的更改来更改它的值。 我试过但不能成功,任何帮助表示赞赏。由于java JComboBox问题

这是我已经试过到目前为止:

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); 
    } 
} 
+0

确定这是一个重复的 – hvgotcodes 2010-11-04 22:38:52

+0

我尝试了exampledepot.com和java2s.com的一些例子,但我无法根据自己的需要使其工作。 – user234194 2010-11-04 22:40:13

+0

你为什么不开始编辑你的发布和发布格式正确的代码。你如何期望我们读取随机格式化的代码。 – camickr 2010-11-04 23:07:10

回答

1
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); 
    } 
} 
0

真的不知道你的问题是什么,你不说。也许问题是

comboBox2.addItem(labelsB); 

这是在加入该列表中的数组作为一个单一的项目,完全可以接受的假设它是正确的,但我猜你想通过数组迭代,并添加每一个作为单独项目。您可能需要删除取消选中的项目。

我假设您尝试从第一个列表中的多个选择(基于您的selectedString操作),如果不是您的代码是英里外?如果你是你不想要的if/else结构,只是多if小号

另外,不要使用(selectedString(is)=="A"),你可能会得到幸运的,但你应该使用`“A” .equals(selectedString(是))