简体   繁体   English

具有自定义类的ArrayList到jList

[英]ArrayList with custom class to jList

I have ArrayList of Children and has a few properties ( full name (String),age(int),parent_id(int),id(int)). 我有ChildrenArray并具有一些属性(全名(String),age(int),parent_id(int),id(int))。 I'm using netbeans design view to create JFrame ( it's more easy for me to position elements). 我正在使用netbeans设计视图来创建JFrame(对我来说定位元素更容易)。

In this JFRAME I have jList how I can set all rows from ArrayList at jList and format like that : 在这个JFRAME中,我有了jList,该如何设置ArrayList中jList的所有行,并设置如下格式:

String.format("%s \\t %d",ch.getFullName(),ch.getAge()) . String.format("%s \\t %d",ch.getFullName(),ch.getAge())

I tried to make it foreach element from arraylist but doesnt work. 我试图使其从arraylist的foreach元素,但不起作用。

Add all the Children classes to the JList model and define a custom ListCellRenderer . 将所有Children类添加到JList模型,并定义一个自定义ListCellRenderer In the renderer get value and use the Children element to set text. 在渲染器中获取值,并使用Children元素设置文本。

A JTable is a better choice for rendering multiple columns of data. JTable是呈现多列数据的更好选择。 See the section from the Swing tutorial on How to Use Tables for more information. 有关更多信息,请参见Swing教程中有关如何使用表的部分。

The DefaultTableModel doesn't support an ArrayList with custom Objects so you could try using the Row Table Model . DefaultTableModel不支持带有自定义对象的ArrayList,因此您可以尝试使用行表模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM