JCombobox 有可能有 2 个 ItemListeners 吗?
Is possible that a JCombobox has 2 ItemListeners?
我有 3 个 JComboBox。第一个显示孩子的学年。第二个显示孩子的 class,第三个显示那个学年的 children 和 class。我希望当我 select 第一个 JComboBox 的一个选项时,在第二个 JComboBox 出现一个选项或另一个(取决于第一个 JComboBox 的 selection)。问题还在于,我希望在第二个 JComboBox 中 select 有一个选项,而在第三个 JComboBox 中出现一个或另一个选项(取决于第二个 JComboBox 的 selection)。我已经尝试了很多,但我不知道该怎么做。我也尝试过 actionlisteners,但没有用。拜托,我将不胜感激。
下面的示例显示了如何根据第一个组合框中的选择填充第二个组合框:
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();
}
});
}
}
我将尝试解释一下我的代码。我收到另一个 class 一个 JSon 对象,因为信息在数据库中。其他 classes 是 ClasInf 和 StudentsInf。这是一个 JSon 对象,所以我必须获取该对象,然后将其更改为字符串,以便将其放在 jcombobox 上。所有的JCombobox都在同一个class中。如果您不明白,请告诉我。我试着翻译了整个答案
这是我的第一个 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);
我有 3 个 JComboBox。第一个显示孩子的学年。第二个显示孩子的 class,第三个显示那个学年的 children 和 class。我希望当我 select 第一个 JComboBox 的一个选项时,在第二个 JComboBox 出现一个选项或另一个(取决于第一个 JComboBox 的 selection)。问题还在于,我希望在第二个 JComboBox 中 select 有一个选项,而在第三个 JComboBox 中出现一个或另一个选项(取决于第二个 JComboBox 的 selection)。我已经尝试了很多,但我不知道该怎么做。我也尝试过 actionlisteners,但没有用。拜托,我将不胜感激。
下面的示例显示了如何根据第一个组合框中的选择填充第二个组合框:
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();
}
});
}
}
我将尝试解释一下我的代码。我收到另一个 class 一个 JSon 对象,因为信息在数据库中。其他 classes 是 ClasInf 和 StudentsInf。这是一个 JSon 对象,所以我必须获取该对象,然后将其更改为字符串,以便将其放在 jcombobox 上。所有的JCombobox都在同一个class中。如果您不明白,请告诉我。我试着翻译了整个答案
这是我的第一个 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);