所以這裏是我的問題:我目前使用兩個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);
}
你已經發布了很多代碼,大部分代碼與手頭的問題無關,這意味着它只會讓我們感到困惑。考慮削減一下你的代碼,以便它只包含與問題相關的代碼和一些代碼,以使它成爲一個自給自足的小型可編譯和可運行程序,[sscce](http://sscce.org )。 – 2013-04-23 01:09:21
對不起。我已經將它縮減爲更相關的代碼。由於它取決於很多其他的東西,所以我不確定我能做到多足自足。如果可以的話,我會盡量讓它成爲一個小型的可運行程序。 – Wakko45 2013-04-23 03:48:57