单击JButton后取消选择JList项

时间:2015-04-08 12:09:28

标签: java swing jbutton jlist japplet

我尝试选择JList中的项目,点击JButton(也可选择点击JRadioButton),然后将所选项目的值添加到另一个项目JList

问题是,在我点击JButtonJRadioButton后,原始JList中的项目被取消选中,并且添加到目标JList的内容为“null ”

第一批JLists。这些项由JCombobox中的选择指定,并从另一个类中的ArrayList生成值。

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    ChannelList cl = new ChannelList();
    cl.createList();

  //determine JList by JCombobox selecton
    String genre = (String)c.getSelectedItem();

    switch(genre){
    case "Please Select Genre of Channel":
        vt1.clear();
        lchannels.setListData(vt1);
        break;
    case "All Genres":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            vt1.add(cl.chList[i].getChTitle());
            lchannels.setListData(vt1);
        }
        break;
    case "Entertainment":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            if(chGenre == 'e'){
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
            }
        }
        break;
    case "Movie":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            if(chGenre == 'm'){
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
            }
        }
        break;
    case "News/Business":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            if(chGenre == 'n'){
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
            }
        }
        break;
    case "Sci-Fi":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            if(chGenre == 's'){
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
            }
        }
        break;
    case "Sports":
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            if(chGenre == 't'){
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
            }
        }
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

问题解决了,因为我改变了检索值的逻辑。

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    ChannelList cl = new ChannelList();
    cl.createList();

  //determine JList by JCombobox selecton
    genre = c.getSelectedItem().toString();

    if(e.getSource() == c){
        vt1.clear();
        for(int i =0; i < cl.chList.length; i++){
            char chGenre = cl.chList[i].getChGenre();
            switch(genre){
            case "Please Select Genre of Channel":
                lchannels.setListData(vt1);
                break;
            case "All Genres":
                vt1.add(cl.chList[i].getChTitle());
                lchannels.setListData(vt1);
                break;
            case "Entertainment":
                if(chGenre == 'e'){
                    vt1.add(cl.chList[i].getChTitle());
                    lchannels.setListData(vt1);
                }
                break;
            case "Movie":
                if(chGenre == 'm'){
                    vt1.add(cl.chList[i].getChTitle());
                    lchannels.setListData(vt1);
                }
                break;
            case "News/Business":
                if(chGenre == 'n'){
                    vt1.add(cl.chList[i].getChTitle());
                    lchannels.setListData(vt1);
                }
                break;
            case "Sci-Fi":
                if(chGenre == 's'){
                    vt1.add(cl.chList[i].getChTitle());
                    lchannels.setListData(vt1);
                }
                break;
            case "Sports":
                if(chGenre == 't'){
                    vt1.add(cl.chList[i].getChTitle());
                    lchannels.setListData(vt1);
                }
                break;
            }
        }
    }
}