制作了一个自定义 ListCellRenderer:
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
/**
*
* @author Spencer
*/
public class TaskRenderer implements ListCellRenderer {
private Task task;
private JPanel panel = new JPanel();
private JCheckBox checkbox = new JCheckBox();
private JLabel label = new JLabel();
public TaskRenderer() {
panel.add(checkbox);
panel.add(label);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
task = (Task) value;
label.setText(task.getName());
return panel;
}
}
使用上述类呈现其中每个单元格的 JList,但无法单击面板中每个单元格的复选框。认为它与它没有得到焦点有关。有任何想法吗?
谢谢,斯宾塞