我有3個JComboBoxes。第一個顯示了一個孩子的學年。第二個顯示孩子的階級,第三個顯示該學年和該班的孩子。我希望當我選擇第一個JComboBox的選項時,第二個JComboBox出現一個或另一個選項(取決於第一個JComboBox的選擇)。問題在於,我想在第二個JComboBox中選擇一個選項,在第三個中出現一個或另一個選項(取決於第二個JComboBox的選擇)。我已經嘗試了很多,我不知道該怎麼做。我也嘗試過actionlisteners,但它沒有奏效。請,我會很感激任何幫助。JCombobox有2個ItemListeners可能嗎?
0
A
回答
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);
相關問題
- 1. 不太明白JComboBox itemListeners
- 2. 2個「並行」的wxpython GUI可能嗎?
- 3. 單個git分支可能有2個名字嗎?
- 4. uml兩個類可能有超過2的關係嗎?
- 5. 在MySQL查詢中可能有2個限制嗎?
- 6. JPanel可以放在JComboBox中嗎?
- 7. 我可以在2列上有2個唯一鍵,但不能組合嗎?
- 8. 使用RxJava 2.x有可能使用Zip超過2個Observable嗎?
- 9. 如何動態添加ItemListeners?
- 10. Rails有可能嗎?
- 11. RevSlider有可能嗎?
- 12. document.obj.write()..有可能嗎?
- 13. ,這有可能嗎?
- 14. AngularJS有可能嗎?
- 15. iOS - RemoteIO AudioUnits,可能有2個?
- 16. 2個功能同時執行。可能嗎?
- 17. 可用性功能JComboBox的內JTable中
- 18. 爪哇 - 這是不可能從JComboBox中
- 19. 可編輯JComboBox
- 20. 兩個羣集有可能重疊嗎?
- 21. Highcharts:有多個傳說可能嗎?
- 22. rails find_by三個字段 - 有可能嗎?
- 23. JPA @NamedQuery有兩個表,可能嗎?
- 24. blur.js有兩個可能的實例嗎?
- 25. 循環中可能有多個鍵嗎?
- 26. 我有2個while循環,但我只想調用一個。可能嗎?
- 27. 在Flex中將2個源拖放到1個目標,有可能嗎?
- 28. SQL查詢與2可能嗎?
- 29. SQL查詢組合2列可能嗎?
- 30. 是否有可能將JComboBox鏈接到SQL Server數據庫
從你的描述,你爲什麼認爲你需要每個組合框一個以上的項目監聽器?另外,你有嘗試過什麼嗎? –
向我們展示2'ComboBox'嘗試的代碼,其中一個限制另一個的選擇。確保我們可以自己運行它。 – user1803551