ListSelectionListener failing to recognize union in if condition

82 views Asked by At

I have 2 jlists and 1 jbutton. What I want to do is when the user selects some items in one jlist, then clicks the button and some elements appear on the other jlist depending on the selection. My problem is with the && condition, cause the program recognizes irst the || and ignores it. So when the user selections "a" and "c", the 3rd if that adds all elements does not correspond. How could I fix this?

Here is my code:

public class test extends JPanel implements ActionListener {

static JFrame frame = new JFrame("");

public test() {

    String[] feedStrings = { "a", "b", "c", "d" }

    final JList feedList = new JList(feedStrings);

                                             feedList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    final DefaultListModel model = new DefaultListModel();

            final JList prodList = new JList(model);


            prodList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    JButton arrow = new JButton("button");

    arrow.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    feedList.addListSelectionListener(new ListSelectionListener() {

                        @Override
                        public void valueChanged(ListSelectionEvent e)
                        {           
                        List<String> templist = new ArrayList<String>();

                        templist = feedList.getSelectedValuesList();

                        if (templist.contains("a")|| templist.contains("b"){

                                         model.removeAllElements();
                                model.addElement("x");
                                model.addElement("y");
                                model.addElement("z");


                }else if(templist.contains("c") ||templist.contains("d")  ) {
                                            model.removeAllElements();
                                model.addElement("t");
                                model.addElement("g");
                                model.addElement("h");
                                model.addElement("k");                  

                }else if(templist.contains("a") && templist.contains("c") {
                                            model.removeAllElements();
                                            model.addElement("t");
                                model.addElement("g");
                                model.addElement("h");
                                model.addElement("k");
                                            model.addElement("x");
                                model.addElement("y");
                                model.addElement("z");  
                        }
                    });

                }
            }); 


        JPanel flow = new JPanel(new FlowLayout());
            flow.add(feedList);
            flow.add(arrow);
            flow.add(prodList);

                    frame.add(flow);


}

private static void createAndShowGUI() {


            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
            frame.setSize(500, 500);
            JFrame.setDefaultLookAndFeelDecorated(true);
            //Add content to the window.
            frame.add(new test());
            frame.setResizable(false);

            frame.pack();
            frame.setVisible(true);
        }

public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
            //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.TRUE);


            try {
                createAndShowGUI();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                }
            });

    }
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }

}
1

There are 1 answers

0
Keerthivasan On BEST ANSWER

You have to make the else if as if loops. If you use if and else if together in your code, the code will get executed only with first condition which turns true and it will not check the other else if loops. In your case, when you choose 'a' and 'c' , the first if condition only will get executed, the remaining else if loops will be ignored