[英]Having Trouble With JComboBox and TreeMap
我的探針是我想將地圖的鍵顯示為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());
}
}
再次感謝您的幫助,我覺得這是我沒有看到的一個小錯誤。 另外,任何人看到的任何語法錯誤都請告訴我(這是我的寵兒)。
我將ArrayList轉換為數組,並默認將其添加到combobox構造函數
public JComboBox generateCombo (Arraylist<String> toConvert){
String[] arrayListToArray = toConvert.toArray(new String[toConvert.size()]);
JComboBox testbox = new JComboBox(arraylistToArray);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.