2013-03-05 81 views
0

我想加载2个组合框; Secoond组合框必须在第一个组合框更改后加载。我正在使用netbeans,我尝试了几次,但它不起作用... 要加载的项目必须与第一个组合中所选项目的exceptio相同。JAVA NetBeans组合框问题

private void firstTeamComboBoxItemStateChanged(java.awt.event.ItemEvent evt) 
{             
     loadSecondTeamComboBox(); 
    }             

    private void loadSecondTeamComboBox() 
    { 
     String[] theTeamsInTheLeague2 = league.loadTeamsInLeague(secondTeam.getLeague()); 
     secondTeamComboBox.addItem("Select a Team"); 
     for(int i = 0; i < theTeamsInTheLeague2.length; i++) 
      if (!(theTeamsInLeague2[i].equals(firstTeam.getLeague())) 
       secondTeamComboBox.addItem(theTeamsInTheLeague2[i]); 
    } 


    private void loadFirstTeamComboBox() 
    { 
     String[] theTeamsInTheLeague1 = league.loadTeamsInLeague(firstTeam.getLeague()); 
     firstTeamComboBox.addItem("Select a Team"); 
     for(int i = 0; i < theTeamsInTheLeague1.length; i++) 
      firstTeamComboBox.addItem(theTeamsInTheLeague1[i]); 
    } 
+0

那么说实话,我不打算在这个相关的答案发生了什么 – Alpan67 2013-03-05 16:23:23

+0

什么是不工作?你的代码的结果是什么? – Guido 2013-03-05 16:29:08

+0

问题是,这两个,firstTeamCombo和secondTeamCombo加载相同的项目,并在同一时刻...我想第二tecomCombo加载后,选择了第一个Teamcommbo的项目... – Alpan67 2013-03-05 16:38:25

回答

1

一种方法是将覆盖DefaultComboBoxModelsetSelectedItem()和保持一个参考otherTeamModel,从allTeamsInTheLeague需要更新它。

class MyComboBoxModel extends DefaultComboBoxModel { 
    private DefaultComboBoxModel otherTeamModel; 

    public MyComboBoxModel(DefaultComboBoxModel otherTeamModel) { 
     this.otherTeamModel = otherTeamModel; 
    } 
    @Override 
    public void setSelectedItem(Object item) { 
     super.setSelectedItem(item); 
     otherTeamModel.removeAllElements(); 
     // add all allTeamsInTheLeague except item 
    } 
}