我正在尝试制作一个自定义 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;
}
}
最佳答案
这解决了我的问题,而且可能效率更高。谢谢大家!
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;
}
}
关于Java Swing Cust ListCellRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29733829/