0

カスタム ListCellRenderer を書きたいと思います。

デフォルトのものと異なる必要があるのは、 の戻り値を表示するのではなく、 の戻り値を表示することだけvalue.toString()ですvalue.myOwnCustomMethodThatReturnsString()

それを行う最も簡単な方法は何ですか?

これがすべて含まれているクラスは、すでに ListCellRenderer を実装しており、次のものがあります。

public Component getListCellRendererComponent(JList<? extends Chapter> list,
        Chapter value, int index, boolean isSelected, boolean cellHasFocus)
{
    return null;
}

括弧の間に何を入れればいいのかわかりません...

4

1 に答える 1

2

最も簡単な方法は次のとおりです。

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;
    }
}
于 2013-09-19T13:48:11.880 に答える