I've found a problem when placing a JCheckBox in a custom ListCellRenderer, some of the other paneals dissapear and I don't now why.
Here's my Renderer:
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;
}
}
The description JLabel dissappears, and another problem is that when I select multiple items they add up, I only want to select one item each time someone clicks.
Any idea what's wrong?
Updated the class to handle TaskListRenderer
And this is the Task Class
For testing purpose added Main method to the TaskRenderer, feel free to remove if not needed