2014-01-15 107 views
0

我的Probem是我想要將我的地圖的鍵顯示爲JComboBox,並且它只顯示其中一個,這些代碼只是我遇到問題的部分,如果讀者不在代碼中看到問題,我也會在這裏列出其他類。所以基本上我有我的鍵作爲一個Instuctor對象和值作爲一組Student對象(一個教師有不止一個學生),當我創建JComboBox它只顯示其中一個鍵。我嘗試了不同的順序,因爲我想也許它只是使用最後一個鍵添加(如鍵被取代),但這不是我可以看到的問題。無論如何,這是越來越冗長,所以這裏是我的代碼。使用JComboBox和TreeMap時遇到問題

//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

顯示你'Instructor'類的代碼,我想在這個類的例外。如果被覆蓋,我需要看到'compareTo()'實現和'hashCode()'方法。 – alex2410

+0

哪裏是'theInstructors' –

+0

theInstructors是一個ArrayList Brent

回答

0

我想ArrayList的轉換爲數組,默認情況下將其添加到組合框構造

public JComboBox generateCombo (Arraylist<String> toConvert){ 
    String[] arrayListToArray = toConvert.toArray(new String[toConvert.size()]); 
    JComboBox testbox = new JComboBox(arraylistToArray); 
} 
+0

我得到錯誤「不能從對象轉換[]到字符串[],我已經試過你的方法(有點不同,而不是我返回一個String [])和初始化我試着用它來初始化JComboBox,並試着將它拋出,並得到一個RunTime錯誤ClassCastException。 – Brent

+0

對不起... toConvert.toArray(new String [toConvert.size()]);修改我的解決方案 –

+0

這擺脫了空指針異常,謝謝。我唯一的問題是我的JComboBox只顯示一個項目,而不是全部三個(Instructors Name3)。對這個問題有任何想法或者我應該問另一個問題 – Brent