我正在尝试制作一个自定义的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;
}
}发布于 2015-04-20 02:50:10
这解决了我的问题,而且可能效率更高。谢谢大家!
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;
}
}https://stackoverflow.com/questions/29733829
复制相似问题