我正在尝试创建一个自定义JList,它显示一个任务列表,其中每个任务表示如下 - 类型:由3个不同的图标表示,状态由2个图标和名称作为简单字符串。
我的问题是,在我突出显示该项目之前,自定义CellRenderer不会使用列表中项目的相应图标。现在我改了一下它总是使用相同的图标(task_small.png)我相信我传递的是正确的数据。
此外,如果您对如何以其他方式改进此代码提出建议/批评,我将很乐意听到它。我不确定使用JPanel是否是正确的选择。
//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;
}
}
答案 0 :(得分:1)
这解决了我的问题,可能效率更高。谢谢大家!
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;
}
}