2010-11-04 85 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(是))