-1

What I tried is to select an item from a JList, click a JButton (optionally also click a JRadioButton), and then the value of selected item will be added to another JList.

The problem is that after I clicked the JButton or JRadioButton, the item in original JList was deselected, and what was added to the destination JList was "null".

first JLists. The items are specified by the selection from a JCombobox, and generate the values from an ArrayList in another class.

@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;
    }
}
hanabi_noir
  • 197
  • 1
  • 1
  • 15
  • 3
    for better help sooner post an SSCCE/MCVE, short, runnable, compilable, with hardcoded value for JList/XxxListModel in local variable – mKorbel Apr 08 '15 at 12:41
  • If anything, at the very least you should provide the code related to the JList configuration. It's necessary to understand the problem. But ideally, you should indeed provide a small example to reproduce your problem, that we can compile to see the issue without having to go through guesswork. – Gnoupi Apr 08 '15 at 13:11

1 Answers1

0

The problem is solved, as I changed the logic of retrieving values.

@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;
            }
        }
    }
}
hanabi_noir
  • 197
  • 1
  • 1
  • 15
  • try to work more with real objects! do not use **char** for **getChGenre()**. create a simple class for your genre and put the object of this class in combobox and in chanellist! this way is much more easy to work with. you could reduce your code to 10-15 lines and it were also more generic . do your really want to add **new code** for each **new genre**? – Ben Apr 10 '15 at 16:37
  • Actually, there's too much repeating and it is easy to make mistake. As I know, in mysql, I can us a syntax like "select chTitle from channelist where chGenre = 'e'", and I can create a class or method to determine which genre to be selected. Is there a similar way in Java? – hanabi_noir Apr 11 '15 at 20:27
  • you can try to use a map. this map contains Genre(Sci-Fi) and chGenre(s). then ask the map. – Ben Apr 13 '15 at 12:50