单击 JButton 后取消选择 JList 项目

JList item deselected after clicking a JButton

我尝试的是 select 来自 JList 的项目,点击 JButton(也可以点击 JRadioButton),然后 JRadioButton 的值=27=]编辑的项目将被添加到另一个 JList

问题是在我点击 JButtonJRadioButton 后,原来 JList 中的项目被删除selected,而目标中添加了什么JList 是 "null"。

第一个 JLists。这些项目由来自 JCombobox 的 selection 指定,并从另一个 class.

中的 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;
    }
}

问题解决,修改取值逻辑

@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;
            }
        }
    }
}