我是Swing
刚接触框架的人。我要设计以下清单。
有什么方法可以让我们自定义列表项JList
?如自定义写Adapter
在Android
,它有一个方法getView()
,这将返回View
到显示为列表项...
您将需要一个自定义渲染器。您可以使用DefualtListCellRenderer
将aJLabel
用作渲染组件,也可以创建自定义ListCellRenderer
,在其中可以通过扩展组件和impelenting来指定渲染组件ListCellRenderer
。
您可以看到更多的提供自定义渲染器。链接指向“如何使用组合框”教程,但组合框JLists
使用相同的类型渲染器。
您将发现的基本概念是,您将不得不重写该方法
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
用于自定义ListCellRenderer
或用于DefaultListCellRenderer
。值将返回每个列表项的值。列表项可以是一个对象,例如Person
带有字段id
和的a name
。因此,您可以将值设置为casePerson
并使用必要的字段来呈现组件。
Person person = (Person)value;
// renderer the component using Person values
// lastly return the component.
因此,基本上可以将其分解为“您将如何渲染其中一个单元格?” 您将使用哪些组件?然后只需实现renderer类/方法
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句