我想编写一个自定义 ListCellRenderer。
唯一需要与默认不同的是,它不显示 value.toString()
的返回值,而是显示 value.myOwnCustomMethodThatReturnsString() 的返回值
.
最简单的方法是什么?
所有这些所在的类已经实现了 ListCellRenderer 并且我已经:
public Component getListCellRendererComponent(JList<? extends Chapter> list,
Chapter value, int index, boolean isSelected, boolean cellHasFocus)
{
return null;
}
我只是不知道括号里该放什么...
最佳答案
最简单的方法是:
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;
}
}
关于java - 编写自定义 ListCellRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18896345/