Java JComboBox和TreeMap有问题
我的问题是我想把我地图的键显示为一个JComboBox,它只显示其中一个,这段代码就是我遇到问题的部分,如果读者在我的代码中没有看到问题,我也会在这里列出其他类。基本上我把我的键作为一个instructor对象,把值作为一组Student对象,一个讲师有多个Student,当我创建JComboBox时,它只显示一个键。我尝试了不同的顺序,因为我认为可能只是使用了最后添加的键,就像更换了键一样,但这不是我能看到的问题。无论如何,这是越来越长,所以这里是我的代码Java JComboBox和TreeMap有问题,java,jcombobox,treemap,Java,Jcombobox,Treemap,我的问题是我想把我地图的键显示为一个JComboBox,它只显示其中一个,这段代码就是我遇到问题的部分,如果读者在我的代码中没有看到问题,我也会在这里列出其他类。基本上我把我的键作为一个instructor对象,把值作为一组Student对象,一个讲师有多个Student,当我创建JComboBox时,它只显示一个键。我尝试了不同的顺序,因为我认为可能只是使用了最后添加的键,就像更换了键一样,但这不是我能看到的问题。无论如何,这是越来越长,所以这里是我的代码 //Test Objects
//Test Objects
TreeSet<Student> inst1Student = new TreeSet<Student>();
TreeSet<Student> inst2Student = new TreeSet<Student>();
TreeSet<Student> inst3Student = new TreeSet<Student>();
inst1Student.add(new Student("Jane Doe"));
inst2Student.add(new Student("Jhon Smith"));
inst3Student.add(new Student("Students Name"));
Instructor inst1 = new Instructor("Instructors Name1", inst1Student);
Instructor inst2 = new Instructor("Instructors Name3", inst2Student);
Instructor inst3 = new Instructor("Instructors Name3", inst3Student);
theList.put(inst1, inst1.getStudents());
theList.put(inst3, inst3.getStudents());
theList.put(inst2, inst2.getStudents());
//Make combo box
instructors = new JComboBox<>();
getInstructorsArrayList();
for (int i = 0; i < theInstructors.size(); i++){
System.out.println(theInstructors.get(i));
instructors.addItem(theInstructors.get(i));
}
panel3.add(instructors);
instructors.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You Clicked Someone");
}
});
这里引用的是我的getInstructorArrayList方法
public static void getInstructorsArrayList() {
for (Entry<Instructor, Set<Student>> entry : theList.entrySet()) {
if (entry.getKey() != null) {
theInstructors.add(entry.getKey().getName());
}
}
}
讲师课程:
package psl_Tracker;
import java.util.Set;
public class Instructor implements Comparable<Instructor> {
private static String name = null;
private static Set<Student> students = null;
public Instructor(String name, Set<Student> students) {
Instructor.name = name;
if (students != null) {
Instructor.students = students;
}
}
public String getName() {
return name;
}
public Set<Student> getStudents() {
return students;
}
@Override
public int compareTo(Instructor other) {
return getName().compareTo(other.getName());
}
}
再次感谢您的帮助,我觉得这是一个我没有看到的小错误。还有任何语法错误,请告诉我这是我的心病。我会将ArrayList转换为数组,并在默认情况下将其添加到combobox构造函数中
public JComboBox generateCombo (Arraylist<String> toConvert){
String[] arrayListToArray = toConvert.toArray(new String[toConvert.size()]);
JComboBox testbox = new JComboBox(arraylistToArray);
}
出示你的讲师课程代码,我想你在那门课上是例外。我需要查看compareTo Implementation和hashCode方法(如果被覆盖)。其中是指令指令指令是ArrayList我仍然无法将错误从object[]转换为string[],我尝试了与此稍有不同的方法,但我返回了string[]并初始化了JComboBox,我最初需要在此处初始化它。我也尝试过强制转换它,但我得到一个运行时错误ClassCastException。对不起。。。toConvert.toArraynew字符串[toConvert.size];修改了我的解决方案这消除了空指针异常,谢谢。现在我唯一无法解决的另一个问题是,我的JComboBox只显示一个项目,而不是所有三个讲师的名字3。对这个问题有什么想法吗?或者我应该问另一个问题。你有没有试着简单地打印ArrayList来看看里面有什么?是的,我打印了,里面只有名为3的讲师,最后我用Netbeans重新编写了代码,这对我来说很有效。