将JCheckBox添加到自定义ListCellRenderer

时间:2017-11-21 10:41:11

标签: java swing jcheckbox listcellrenderer

我在将JCheckBox放入自定义ListCellRenderer时发现了一个问题,其他一些平台消失了,我现在不知道原因。

这是渲染器

public class TaskRenderer extends JPanel implements ListCellRenderer<Task> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel lbEdit = new JLabel();
    private JLabel lbCheck = new JLabel();
    private JLabel lbTitle = new JLabel();
    private JLabel lbDescription = new JLabel();
    private JPanel panelText;
    private JPanel panelEdit;
    private JPanel panelCheck;
    private JCheckBox checkTask;

    public TaskRenderer() {

        setLayout(new BorderLayout(5, 5));

        panelText = new JPanel(new GridLayout(0, 1));
        lbTitle.setFont(lbTitle.getFont ().deriveFont (20.0f));
        lbDescription.setFont(lbDescription.getFont ().deriveFont (0.0f));
        panelText.add(lbTitle);
        panelText.add(lbDescription);

        panelEdit = new JPanel();
        panelEdit.setBorder(new EmptyBorder(5, 5, 5, 5));
        panelEdit.add(lbEdit);

        panelCheck = new JPanel();
        panelCheck.setBorder(new EmptyBorder(5, 5, 5, 5));
        checkTask =  new JCheckBox("Tarea completada");
        checkTask.setSelected(false);
        panelCheck.add(checkTask);

        JPanel p = new JPanel(new BorderLayout());
        p.add(panelEdit,BorderLayout.WEST);
        p.add(panelCheck,BorderLayout.CENTER);


        add(p, BorderLayout.WEST);
        add(panelText, BorderLayout.CENTER);
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Task> list,
            Task task, int index, boolean isSelected, boolean cellHasFocus) {

        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); 

        ImageIcon editIcon = new ImageIcon("images/edit.png"); // load the image to a imageIcon
        Image image2 = editIcon.getImage(); // transform it 
        Image newimg2 = image2.getScaledInstance(15, 15,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
        editIcon = new ImageIcon(newimg2);  // transform it back



        lbEdit.setIcon(editIcon);
        lbTitle.setText(task.getTitle());
        lbDescription.setText(task.getDescription());
        lbDescription.setForeground(Color.blue);

        // set Opaque to change background color of JLabel
        lbTitle.setOpaque(true);
        lbDescription.setOpaque(true);
        lbEdit.setOpaque(true);

        // when select item
        if (isSelected) {
            lbTitle.setBackground(list.getSelectionBackground());
            lbDescription.setBackground(list.getSelectionBackground());
            lbEdit.setBackground(list.getSelectionBackground());
            if(task.getPriority() == 1) {
                setBackground(Color.RED);
            }else if (task.getPriority() == 2) {
                setBackground(Color.YELLOW);
            }else {
                setBackground(Color.GREEN);
            }
            panelEdit.setBackground(list.getSelectionBackground());
        } else { // when don't select
            if(task.getPriority() == 1) {
                setBackground(Color.RED);
            }else if (task.getPriority() == 2) {
                setBackground(Color.YELLOW);
            }else {
                setBackground(Color.GREEN);
            }
        }
        return this;
    }
}

JLabel说明消失了,另一个问题是,当我选择多个项目时,我只想在每次点击时选择一个项目。

知道什么是错的吗?

1 个答案:

答案 0 :(得分:0)

更新了处理TaskListRenderer的类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

public class TaskRenderer extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JList taskList = new JList();
    private JLabel lbCheck = new JLabel();
    private JLabel lbTitle = new JLabel();
    private JLabel lbDescription = new JLabel();
    private JPanel panelText;
    private JPanel panelEdit;
    private JPanel panelCheck;
    private JCheckBox checkTask;

    public TaskRenderer() {

        setLayout(new BorderLayout(5, 5));

        panelText = new JPanel(new GridLayout(0, 1));
        lbTitle.setFont(lbTitle.getFont().deriveFont(20.0f));
        lbDescription.setFont(lbDescription.getFont().deriveFont(0.0f));
        panelText.add(lbTitle);
        panelText.add(lbDescription);

        panelEdit = new JPanel();
        panelEdit.setBorder(new EmptyBorder(5, 5, 5, 5));
        taskList = new JList<Task>(new Task[] {
                new Task("Task1", "T1 Desc", 1, false),
                new Task("Task2", "T2 Desc", 2, false),
                new Task("Task3", "T3 Desc", 3, false),
                new Task("Task4", "T4 Desc", 4, false),
                new Task("Task5", "T5 Desc", 5, false) });

        taskList.setCellRenderer(new TaskListRenderer());
        taskList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        // Add a mouse listener to handle changing selection

        taskList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
                JList<Task> list = (JList<Task>) event.getSource();

                // Get index of item clicked

                int index = list.locationToIndex(event.getPoint());
                Task item = (Task) list.getModel().getElementAt(index);

                // Toggle selected state

                item.setSelected(!item.isSelected());

                // Repaint cell

                list.repaint(list.getCellBounds(index, index));
            }
        });

        panelEdit.add(new JScrollPane(taskList));

        panelCheck = new JPanel();
        panelCheck.setBorder(new EmptyBorder(5, 5, 5, 5));
        checkTask = new JCheckBox("Tarea completada");
        checkTask.setSelected(false);
        panelCheck.add(checkTask);

        JPanel p = new JPanel(new BorderLayout());
        p.add(panelEdit, BorderLayout.WEST);
        p.add(panelCheck, BorderLayout.CENTER);

        add(p, BorderLayout.WEST);
        add(panelText, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        TaskRenderer ren = new TaskRenderer();
        JFrame frame = new JFrame();
        frame.add(ren);
        frame.pack();
        frame.setVisible(true);
    }

    class TaskListRenderer extends JCheckBox implements
            ListCellRenderer<Task> {

        @Override
        public Component getListCellRendererComponent(
                JList<? extends Task> list, Task value, int index,
                boolean isSelected, boolean cellHasFocus) {

            setEnabled(list.isEnabled());
            setSelected(value.isSelected());
            setFont(list.getFont());
            setBackground(list.getBackground());
            setForeground(list.getForeground());
            setText(value.toString());

            System.out.println("Value " + value.getTitle());
            ImageIcon editIcon = new ImageIcon("images/edit.png"); // load the
                                                                    // image
            // to a
            // imageIcon
            Image image2 = editIcon.getImage(); // transform it
            Image newimg2 = image2.getScaledInstance(15, 15,
                    java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
            editIcon = new ImageIcon(newimg2); // transform it back

            // when select item
            if (isSelected) {

                lbTitle.setText(value.getTitle());
                lbDescription.setText(value.getDescription());
                lbDescription.setForeground(Color.blue);

                // set Opaque to change background color of JLabel
                lbTitle.setOpaque(true);
                lbDescription.setOpaque(true);
                taskList.setOpaque(true);

                lbTitle.setBackground(list.getSelectionBackground());
                lbDescription.setBackground(list.getSelectionBackground());
                taskList.setBackground(list.getSelectionBackground());
                if (value.getPriority() == 1) {
                    setBackground(Color.RED);
                } else if (value.getPriority() == 2) {
                    setBackground(Color.YELLOW);
                } else {
                    setBackground(Color.GREEN);
                }
                panelEdit.setBackground(list.getSelectionBackground());
            } else { // when don't select
                if (value.getPriority() == 1) {
                    setBackground(Color.RED);
                } else if (value.getPriority() == 2) {
                    setBackground(Color.YELLOW);
                } else {
                    setBackground(Color.GREEN);
                }
            }

            return this;
        }
    }

}

这是任务类

public class Task {

    private String title;
    private String description;
    private int priority;
    private boolean isSelected = false;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public Task(String title, String description, int priority,
            boolean isSelected) {
        super();
        this.title = title;
        this.description = description;
        this.priority = priority;
        this.isSelected = isSelected;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public String toString() {
        return this.title;
    }

}

出于测试目的,将Task方法添加到TaskRenderer,如果不需要,可随意删除