2013-04-23 119 views
0

所以這裏是我的問題:我目前使用兩個JComboBoxes。第二個JComboBox假定根據第一個JComboBox中選擇的內容進行更新。在這種情況下,第一個JComboBox包含一個主要研究的縮寫:(ART,CSC,MTH,PHY等),第二個JComboBox假設更新爲該特定專業的所有可能的類編號。所以我擁有的文件閱讀器能夠讀取所有信息並相應地填寫這兩個列表。我遇到的第一個問題是在從第一個JComboBox中選擇特定專業時進行第二次JComboBox更新。我通過向第一個JComboBox添加一個actionListener來解決這個問題。但現在我有一個問題..:JComboBox不刷新/更新由於ActionListener

問題:當我點擊第一個JComboBox中有像應該有,但是當我點擊的專業之一,它不會改變所有大滿貫賽的名單。第二個JComboBox會改變,就像我選擇了正確的那個一樣,但是第一個JComboBox不會改變。它只停留在「藝術」(首先按字母順序排列)。

我選擇從第一JComboBox的其他選項之一: http://imageshack.us/photo/my-images/845/problem2t.jpg/

它適當地改變第二JComboBox的,但第一個也不會改變。 http://imageshack.us/photo/my-images/189/problem1n.jpg/

這裏是我的代碼:首先我有我的構造函數來設置圖形。然後我有我的動作偵聽器(JComboBox是最後一個elseif語句),然後我有我的方法來設置JComboBoxes。與您的代碼

public Student(){ 
     //Window Attributes 
     setSize(WINDOW_WIDTH,WINDOW_HEIGHT); 
     setTitle("Academic Major Selection"); 

     //Center the Program Window 
     Dimension dim = new Dimension(toolkit.getScreenSize()); 
     setLocation((int)dim.getWidth()/2 - WINDOW_WIDTH/2,(int)dim.getHeight()/2 - WINDOW_HEIGHT/2); 

     //do not allow resizing of window 
     setResizable(false); 

     //create arrays for classes 
     //input file reader here 
     String[] subjectArray = {"CSC","MTH","ENG","THE","PHY"}; 
     String[] numberArray = {"100","200","300","400","500"}; 


     //create boxes for dropdown 
     subjectBox = new JComboBox(subjectArray); 
     subjectBox.addActionListener(this); 
     numberBox = new JComboBox(numberArray); 

     //creates a panel for our dropdown panels 
     selectPanel = new JPanel(); 
     selectPanel.setLayout(new GridLayout(1,2,25,0)); 
     selectPanel.add(subjectBox); 
     selectPanel.add(numberBox); 
      . 
      . 
      .//trimmed out some excess stuff 
      . 
     try { 
      setDropDownBoxes(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //show the window 
     setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     //What button is pressed? 
     . 
      . 
      .//Trimmed out some extra stuff 
      . 
      //This will equal true when something is selected from the first JComboBox 
     }else if(((String)subjectBox.getSelectedItem()).length() == 3){ 
      System.out.println("New Subject Selected"); 
      try { 
       setDropDownBoxes(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } 

    } 

    void setDropDownBoxes() throws IOException{ 
     int j=0; 

     String[][] someArrayTwoD = studentWriter.fetchClassLists(); 

     String[] subjectArray = new String[someArrayTwoD.length]; 
     while(j<someArrayTwoD.length){ 
      subjectArray[j] = someArrayTwoD[j][0]; 
      j++; 
     } 

     String[] numberArray = new String[someArrayTwoD[subjectBox.getSelectedIndex()].length-1]; 
     for(int i=0; i<numberArray.length; i++){ 
      numberArray[i] = someArrayTwoD[subjectBox.getSelectedIndex()][i+1]; 
     } 

     selectPanel.remove(subjectBox); 
     selectPanel.remove(numberBox); 

     subjectBox = new JComboBox(subjectArray); 
     numberBox = new JComboBox(numberArray); 

     selectPanel.add(subjectBox); 
     selectPanel.add(numberBox); 
     selectPanel.validate(); 
     subjectBox.addActionListener(this); 
    } 
+0

你已經發布了很多代碼,大部分代碼與手頭的問題無關,這意味着它只會讓我們感到困惑。考慮削減一下你的代碼,以便它只包含與問題相關的代碼和一些代碼,以使它成爲一個自給自足的小型可編譯和可運行程序,[sscce](http://sscce.org )。 – 2013-04-23 01:09:21

+0

對不起。我已經將它縮減爲更相關的代碼。由於它取決於很多其他的東西,所以我不確定我能做到多足自足。如果可以的話,我會盡量讓它成爲一個小型的可運行程序。 – Wakko45 2013-04-23 03:48:57

回答

2

的一個問題是,你正在使用==檢查等價的字符串,而不是請使用equals(...)equalsIgnoreCase(...)方法:

if(e.getActionCommand() == addButton.getText()){ 
    //.... 
} 

明白==檢查兩個對象是相同的,而不是你感興趣的。另一方面,方法檢查兩個字符串是否具有相同順序的相同字符,這是重要的。因此,而不是

if (fu == "bar") { 
    // do something 
} 

做,

if (fu.equals("bar")) { 
    // do something 
} 

,或者

if (fu.equalsIgnoreCase("bar")) { 
    // do something 
} 

甚至更​​好,讓你擺脫 「轉板」 的ActionListener的,沒有你的GUI類實現監聽器接口(通常是一個壞主意),而是給你的JButton匿名內部類監聽器。

+1

再次..........:P – MadProgrammer 2013-04-23 01:27:13

+0

是的,謝謝你。這部分實際上是由其他人與我一起編寫程序編寫的,並回顧它,我對任何這些if語句都起作用感到驚訝。我已經將它們全部更改爲.equals(),但問題仍然存在。我會盡快添加圖片。 – Wakko45 2013-04-23 03:51:00

+0

@ Wakko45:如果您真的需要我們的幫助,請再次考慮努力創建併發布[sscce](http://sscce.org)。請檢查鏈接。 – 2013-04-23 04:17:25