简体   繁体   English

Java Swing-Jlabel ActionListener不会打印出选中的复选框

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

I have a JList which allows you to select which options to "cancel". 我有一个JList,它允许您选择要“取消”的选项。 Once the cancel button is clicked, a JLabel prints out which options have been cancelled. 单击取消按钮后,JLabel将打印出已取消的选项。 However, when I try to test the code, and I select the options and click "cancel", the JLabel prints nothing. 但是,当我尝试测试代码并选择选项并单击“取消”时,JLabel不打印任何内容。 So I'm assuming the ActionListener for the cancel button does not work. 所以我假设取消按钮的ActionListener不起作用。 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);

    JPanel panel3 = new JPanel();
    panel3.setLayout(new GridLayout(3,0));
    panel3.add(label3);

    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.add(panel3, BorderLayout.SOUTH);

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


    }
panel3.add(cancelledJobLabel);

When you add/remove components to a visible GUI the basic code is: 当您将组件添加/删除到可见的GUI时,基本代码为:

panel.add(...);
panel.revalidate();
panel.repaint();

The revalidate() invokes the layout manager. revalidate()调用布局管理器。 Otherwise the component has a size of (0, 0) so there is nothing to paint. 否则,组件的大小为(0,0),因此无需绘制任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM