我想写一个定制的ListCellRenderer。
唯一需要与默认值不同的地方是,它不显示value.toString()的返回值,而是显示value.myOwnCustomMethodThatReturnsString()的返回值。
最简单的方法是什么?
所有这些都在实现ListCellRenderer的类中,我有:
public Component getListCellRendererComponent(JList<? extends Chapter> list,
Chapter value, int index, boolean isSelected, boolean cellHasFocus)
{
return null;
}我只是不知道把什么放在括号里.
发布于 2013-09-19 13:48:11
最简单的方法是:
public class MyRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<? extends Chapter> list, Chapter value, int index, boolean isSelected, boolean cellHasFocus)
{
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (c instanceof Jlabel) { // it would work because DefaultListCellRenderer usually returns instance of JLabel
((JLabel)c).setText(value.myOwnCustomMethodThatReturnsString());
}
return c;
}
}https://stackoverflow.com/questions/18896345
复制相似问题