My Probem是我想将我的地图的键显示为JComboBox,它只显示其中一个,这个代码只是我遇到问题的部分,如果读者没有在代码中看到问题我还会在这里列出其他课程。所以基本上我把我的键作为一个Instuctor对象和值作为一组Student对象(一个教师有一个以上的学生),当我创建JComboBox时它只显示其中一个键。我尝试了不同的排序,因为我想也许只是使用了最后添加的密钥(就像密钥被替换了)但这不是我能看到的问题。无论如何,这是越来越长,所以这是我的代码。
//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");
}
});
并且引用它是我拥有的getInstructorsArrayList()方法。
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());
}
}
再一次任何帮助都会受到赞赏,我觉得这是一个我没有看到的错误。任何人看到的任何语法错误请告诉我(这是我的一个宠儿)。
答案 0 :(得分:0)
我会将ArrayList转换为数组并将其默认添加到组合框构造函数
public JComboBox generateCombo (Arraylist<String> toConvert){
String[] arrayListToArray = toConvert.toArray(new String[toConvert.size()]);
JComboBox testbox = new JComboBox(arraylistToArray);
}