gpt4 book ai didi

java - 编写自定义 ListCellRenderer

转载 作者:行者123 更新时间:2023-12-02 06:43:23 24 4
gpt4 key购买 nike

我想编写一个自定义 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com