Reputation: 37
I am trying to make a custom JList, that displays a list of tasks, where each task is represented as follows- type: represented by 3 different icons, status by 2 icons and name as simple String.
My problem is that, at first the custom CellRenderer would not use the appropriate icons for an item in the list until I highlighted the item. Now I changed it a bit and it always uses the same icon (task_small.png) I am sure I am passing in the right data.
Also, if you have advice/criticism on how to improve this code in other ways I will be happy to hear it. I am not sure if using JPanel is the right choice.
//imports...
class TaskListCellRenderer extends JPanel implements ListCellRenderer {
private ClassLoader cl = this.getClass().getClassLoader();
private JLabel statusAndName = new JLabel();
private JLabel icon;
public TaskListCellRenderer() {
setOpaque(true);
setLayout(new BorderLayout());
setBorder(new EmptyBorder(5,5,5,5));
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Task entry = (Task) value;
if (entry.isStatus())
statusAndName.setIcon(new ImageIcon(cl.getResource("finished.png")));
else
statusAndName.setIcon(new ImageIcon(cl.getResource("unfinished.png")));
statusAndName.setText(entry.getName());
statusAndName.setBorder(new EmptyBorder(0,15,0,0));
int type = entry.getType();
System.out.println(type);
if (type == 1)
icon = new JLabel(new ImageIcon(cl.getResource("task_small.png")));
else if (type == 2)
icon = new JLabel(new ImageIcon(cl.getResource("issue_small.png")));
else if (type == 3)
icon = new JLabel(new ImageIcon(cl.getResource("request_small.png")));
//My attempts to make this work- at first I had 3 pre-made Icons which I made icon = to;
remove(statusAndName);
remove(icon);
add(icon, BorderLayout.WEST);
add(statusAndName, BorderLayout.CENTER);
revalidate();
repaint();
if (isSelected) {
setBackground(Constants.blue);
setForeground(Constants.black);
} else {
setBackground(Constants.lightGrey);
setForeground(Constants.black);
}
return this;
}
}
Upvotes: 0
Views: 99
Reputation: 37
This solved my problems and probably is much more efficient. Thank you everyone!
class TaskListCellRenderer extends JPanel implements ListCellRenderer {
private ClassLoader cl = this.getClass().getClassLoader();
private JLabel statusAndName = new JLabel();
private JLabel icon = new JLabel();
private Icon finishedIcon = new ImageIcon(cl.getResource("finished.png"));
private Icon unfinishedIcon = new ImageIcon(cl.getResource("unfinished.png"));
private Icon taskIcon = new ImageIcon(cl.getResource("task_small.png"));
private Icon requestIcon = new ImageIcon(cl.getResource("request_small.png"));
private Icon issueIcon = new ImageIcon(cl.getResource("issue_small.png"));
public TaskListCellRenderer() {
setOpaque(true);
setLayout(new BorderLayout());
setBorder(new EmptyBorder(5,5,5,5));
add(icon, BorderLayout.WEST);
add(statusAndName, BorderLayout.CENTER);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Task entry = (Task) value;
if (entry.isStatus())
statusAndName.setIcon(finishedIcon);
else
statusAndName.setIcon(unfinishedIcon);
statusAndName.setText(entry.getName());
statusAndName.setBorder(new EmptyBorder(0,15,0,0));
int type = entry.getType();
System.out.println(type);
if (type == 1)
icon.setIcon(taskIcon);
else if (type == 2)
icon.setIcon(issueIcon);
else if (type == 3)
icon.setIcon(requestIcon);
if (isSelected) {
setBackground(Constants.blue);
setForeground(Constants.black);
} else {
setBackground(Constants.lightGrey);
setForeground(Constants.black);
}
return this;
}
}
Upvotes: 1