lundi 20 mars 2017

Java Swing - Jlabel ActionListener won't print out selected Checkbox

I have a JList which allows you to select which options to "cancel". Once the cancel button is clicked, a JLabel prints out which options have been cancelled. However, when I try to test the code, and I select the options and click "cancel", the JLabel prints nothing. So I'm assuming the ActionListener for the cancel button does not work. I have included the relevant code below:

public class WarehouseInterface extends JFrame{
private JFrame frame;

public WarehouseInterface(){

    frame = new JFrame("Warehouse Interface");


    DefaultListModel demoList = new DefaultListModel();
    HashMap<String, Job> jobHashMap = SharedInformation.jobs;

    int i = 0;
    ArrayList<String> jobIDs = new ArrayList<>();
    Iterator it = jobHashMap.entrySet().iterator();

    while (it.hasNext() && i < 10) {
        Map.Entry pair = (Map.Entry)it.next();
        Job job = (Job) pair.getValue();    
        String jobID = job.getID();
        demoList.add(i, jobID);
        jobIDs.add(jobID);
        i++;
    }

    JList list = new JList(demoList);
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setCellRenderer(new CheckList());

    JButton cancel = new JButton("Cancel");
    cancel.setFont(new Font("Serif", Font.PLAIN, 14));

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(2,0));
    panel2.add(list);
    panel2.add(cancel);

    ActionListener cancelListener = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){

            for(int index : list.getSelectedIndices()) {
                String jobID = (String) demoList.get(index);
                JLabel cancelledJobLabel = new JLabel(jobID);
                cancelledJobLabel.setPreferredSize(new Dimension(50, 20));
                cancelledJobLabel.setFont(new Font("Serif", Font.PLAIN, 18));
                cancelledJobLabel.setOpaque(true);
                panel3.add(cancelledJobLabel);
                //ERROR HERE :(
            }

        }
    };
    cancel.addActionListener(cancelListener);

    frame.add(panel2, BorderLayout.CENTER);


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


}
    class CheckList extends JLabel implements ListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, 
                        int index, boolean isSelected, boolean hasFocus) {

            setComponentOrientation(list.getComponentOrientation());

            if (isSelected) {
                 setBackground(Color.RED);
                 setForeground(Color.WHITE);

             // unselected, and not the DnD drop location
             } else {
                 setBackground(Color.WHITE);
                 setForeground(Color.BLACK);
             };

            setOpaque(true);
            setEnabled(list.isEnabled());
            setFont(list.getFont());
            setText(value.toString());
            return this;
        }


    }




Aucun commentaire:

Enregistrer un commentaire