简体   繁体   English

JAVA Netbeans的comboBox问题

[英]JAVA netbeans problems with comboBox

i am trying to load 2 combo Boxes; 我正在尝试加载2个组合框; 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. 我正在使用netbeans,但尝试了几次,但无法正常工作...要加载的项目必须与第一个组合中选择的项目的exceptio相同。

    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]);
    }

One approach would be to override setSelectedItem() in DefaultComboBoxModel and keep a reference to otherTeamModel , updating it as needed from allTeamsInTheLeague . 一种方法是重写DefaultComboBoxModel setSelectedItem()并保留对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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM