Reputation: 105
i am trying to load 2 combo Boxes; the secoond combo box has to be loaded after the change in the first combo. I am using netbeans and i tried several times but it does not work ... the items to be loaded have to be the same with the exceptio of the item chosen in the first combo.
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]);
}
Upvotes: 0
Views: 1215
Reputation: 205805
One approach would be to override setSelectedItem()
in DefaultComboBoxModel
and keep a reference to otherTeamModel
, updating it as needed from 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
}
}
Upvotes: 1