2015-05-09 21 views
0

我有3個JComboBoxes。第一個顯示了一個孩子的學年。第二個顯示孩子的階級,第三個顯示該學年和該班的孩子。我希望當我選擇第一個JComboBox的選項時,第二個JComboBox出現一個或另一個選項(取決於第一個JComboBox的選擇)。問題在於,我想在第二個JComboBox中選擇一個選項,在第三個中出現一個或另一個選項(取決於第二個JComboBox的選擇)。我已經嘗試了很多,我不知道該怎麼做。我也嘗試過actionlisteners,但它沒有奏效。請,我會很感激任何幫助。JCombobox有2個ItemListeners可能嗎?

+1

從你的描述,你爲什麼認爲你需要每個組合框一個以上的項目監聽器?另外,你有嘗試過什麼嗎? –

+0

向我們展示2'ComboBox'嘗試的代碼,其中一個限制另一個的選擇。確保我們可以自己運行它。 – user1803551

回答

0

這裏有一個例子,說明如何您可以根據在第一個組合框中選擇填充第二個組合框:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 

public class ComboBoxTwo extends JPanel implements ActionListener 
{ 
    private JComboBox<String> mainComboBox; 
    private JComboBox<String> subComboBox; 
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>(); 

    public ComboBoxTwo() 
    { 
     String[] items = { "Select Item", "Color", "Shape", "Fruit" }; 
     mainComboBox = new JComboBox<String>(items); 
     mainComboBox.addActionListener(this); 

     // prevent action events from being fired when the up/down arrow keys are used 
     mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     add(mainComboBox); 

     // Create sub combo box with multiple models 

     subComboBox = new JComboBox<String>(); 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     add(subComboBox); 

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

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxTwo()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
0

我要去嘗試解釋一下我的代碼。我收到另一個JSon對象的類,因爲這些信息在數據庫中。其他課程是ClasInf和StudentsInf。這是一個JSon對象,所以我必須獲取該對象,然後將其更改爲一個String,以便將其放在jcombobox上。所有JCombobox都在同一班。如果你不明白的話請告訴我。我試圖翻譯整個答案

這是我的第一個JCombobox。

for(int c=0;c<arrayClassroom.size();c++){ 
     JsonObject clas = arrayClassroom.getJsonObject(c); 
     comboBoxYear.addItem(clas.getString("name")); 
    } 

這是第二個和第三

ItemListener itemlistener= new ItemListener() { 
     public void itemStateChanged(ItemEvent eventCombo){ 
      comboBoxYear.removeAllItems(); 
      comboBoxYear.addItem("Class"); 
      clase = new ClasInf(); 
      String cursoPru = comboBoxYear.getSelectedItem().toString(); 
      arrayClas = clase.getclase(cursoPru); 

      for(int c=0;c<arrayClas.size();c++){ 
       JsonObject curso = arrayClas.getJsonObject(c); 
       comboBoxYear.addItem(curso.getString("name_class")); 
      } 

      comboBoxStudents.removeAllItems(); 
      comboBoxStudents.addItem("Students"); 
      students= new StudentsInf(); 
      String clases = comboBoxClase.getSelectedItem().toString(); 
      arrayStudent = student.getStudent(clases); 

      for(int j=0;j<arrayStudent.size();j++){ 
       JsonObject clase = arrayStudent.getJsonObject(j); 
       comboBoxStudents.addItem(clase.getString("nombre")); 
      } 
     } 
    }; 

    comboBoxYear.addItemListener(itemlistener);