如何通过选择第一个和第二个JComboBox的值来改变第二个和第三个JComboBox的值?



我制作了3个相互关联的组合框。如果我选择"对于第一个组合框,第二个组合框应该是" & "(没有其他选项),第三个组合框也是如此。

问题是,它总是说不能调用Object.toString()"因为javax.swing.JComboBox.getSelectedItem()"null"

下面是我的第一个comboBox的代码:

private void dimensiComboBoxActionPerformed(java.awt.event.ActionEvent evt) { 
if("Kepemimpinan".equals(dimensiComboBox.getSelectedItem().toString())){
String jurusan= "Kepemimpinan";
if(kategoriComboBox.getSelectedItem()!=null)
kategoriComboBox.removeAllItems();
//if(egoriComboBox.removeAllItems();null ==kategoriComboBox)

isiMatkul(jurusan);
//kategoriComboBox.addItem("Kategori");
System.out.println(kategoriComboBox.getSelectedIndex());
}//else{matkulComboBox.removeAllItems();}

else if("Kompetensi Individu".equals(dimensiComboBox.getSelectedItem().toString())){
String jurusan= "Kompetensi Individu";
if(kategoriComboBox.getSelectedItem()!=null) kategoriComboBox.removeAllItems();
System.out.println(kategoriComboBox.getSelectedIndex());
isiMatkul(jurusan);

}//else{matkulComboBox.removeAllItems();}

else if("Jiwa Sosial".equals(dimensiComboBox.getSelectedItem().toString())){
String jurusan= "Jiwa Sosial";
System.out.println(kategoriComboBox.getSelectedIndex());
if(kategoriComboBox.getSelectedItem()!=null)kategoriComboBox.removeAllItems();
isiMatkul(jurusan);
}//else{matkulComboBox.removeAllItems();}

else if("Pengembangan Minat dan Kreativitas".equals(dimensiComboBox.getSelectedItem().toString())){
String jurusan= "Pengembangan Minat dan Kreativitas";
System.out.println(kategoriComboBox.getSelectedIndex());
if(kategoriComboBox.getSelectedItem()!=null)kategoriComboBox.removeAllItems();
isiMatkul(jurusan);
}//else{matkulComboBox.removeAllItems();}




}             

第二个组合框的值取决于第一个组合框:

private void kategoriComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
// TODO add your handling code here:
if(kategoriComboBox.getSelectedItem()==null){ 
System.out.println(kategoriComboBox.getSelectedItem().toString());}
else{
if ("Pendidikan".equals(kategoriComboBox.getSelectedItem().toString())) {
String jurusan= "Pendidikan";
subKategoriComboBox.removeAllItems();
//System.out.println(subKategoriComboBox.getSelectedIndex());
isiSubKategori(jurusan);
} //else{matkulComboBox.removeAllItems();}
else if("Ormawa".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Ormawa";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Pelaksana Organisasi".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Pelaksana Organisasi";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Narasumber".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Narasumber";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Prestasi".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Prestasi";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Juara Kompetisi".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Juara Kompetisi";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Peserta Kompetisi".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Peserta Kompetisi";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Olimpiade Perguruan Tinggi Kedinasan".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Olimpiade Perguruan Tinggi Kedinasan";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Kompetisi Internal STIS".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Kompetisi Internal STIS";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Putra-Putri STIS".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Putra-Putri STIS";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Penulis Buku".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Penulis Buku";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Jurnal Ilmiah".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Jurnal Ilmiah";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Menjadi Pembicara/Pelatih".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Menjadi Pembicara/Pelatih";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}//else{matkulComboBox.removeAllItems();}
else if("Panitia Kegiatan Sosial".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Panitia Kegiatan Sosial";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Relawan Kegiatan Sosial".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Relawan Kegiatan Sosial";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Pelatihan dan Pengabdian Masyarakat".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Pelatihan dan Pengabdian Masyarakat";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Kegiatan Sosial Lainnya".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Kegiatan Sosial Lainnya";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Peserta Seminar".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Peserta Seminar";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Keanggotaan UKM/Bidang/Divisi".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Keanggotaan UKM/Bidang/Divisi";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
}
else if("Peserta Kegiatan Terbuka UKM/SEMA".equals(kategoriComboBox.getSelectedItem().toString())){
String jurusan= "Peserta Kegiatan Terbuka UKM/SEMA";
subKategoriComboBox.removeAllItems();
isiSubKategori(jurusan);
} 

}
}  

这"isiSubKategori (jurusan)";和"isiMatkul (jurusan)";

用于将值user selected填充到comboBox中。每次选择第一个组合框时的输出

我是这样写这段代码的:

package com.mycompany.test;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ComboBoxTest extends JPanel {
private JComboBox<String> cb1;
private JComboBox<String> cb2;
private JComboBox<String> cb3;
private int configuring = 0;
public ComboBoxTest() {
cb1 = new JComboBox<>();
cb1.addItem("A");
cb1.addItem("B");
cb1.addItem("C");
cb1.addActionListener((ae) -> {
String selection = (String)cb1.getSelectedItem();
configuring++;
cb2.removeAllItems();
cb2.addItem(selection+"A");
cb2.addItem(selection+"B");
cb2.addItem(selection+"C");
configuring--;
});
add(cb1);
cb2 = new JComboBox<>();
cb2.addActionListener((ae) -> {
String selection = (String)cb2.getSelectedItem();
configuring++;
cb3.removeAllItems();
cb3.addItem(selection+"A");
cb3.addItem(selection+"B");
cb3.addItem(selection+"C");
configuring--;
});
add(cb2);
cb3 = new JComboBox<>();
cb3.addActionListener((ae) -> {
String selection = (String)cb3.getSelectedItem();

if (configuring==0) {
System.out.println("User made up his mind - but that's what we have the Ok button for");
}
});
add(cb3);

// perform the first selection
cb1.setSelectedIndex(0);
}
public String getChoice() {
return (String)cb3.getSelectedItem();
}
public static void main(String[] args) {
ComboBoxTest cbt = new ComboBoxTest();
if (JOptionPane.showOptionDialog(null, cbt, "ComboBoxTest", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null) == JOptionPane.OK_OPTION) {
System.out.println("User chose "+cbt.getChoice()+". Do something with it.");
}
}
}

关键是:不要删除所有项目。如果你删除了所有的项,它将返回null并且发生错误。

这是一个可能对你有帮助的源代码。

public void hapus(JComboBox j, int n){

for(int i=n-1;i>=1;i--){
j.removeItemAt(i);
}
}

相关内容

最新更新