【问题标题】:ArrayList with custom class to jList具有自定义类的 ArrayList 到 jList
【发布时间】:2013-09-29 08:55:35
【问题描述】:

我有 ArrayList of Children 并且有一些属性(全名 (String)、age(int)、parent_id(int)、id(int))。 我正在使用 netbeans 设计视图来创建 JFrame(对我来说定位元素更容易)。

在这个 JFRAME 中,我有 jList,我如何将 ArrayList 中的所有行设置为 jList 和格式:

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

我尝试从 arraylist 中创建 foreach 元素,但没有用。

【问题讨论】:

  • 详细信息不足,请尝试添加更多详细信息。

标签: java swing netbeans arraylist


【解决方案1】:

JTable 是呈现多列数据的更好选择。有关更多信息,请参阅 How to Use Tables 上的 Swing 教程部分。

DefaultTableModel 不支持带有自定义对象的 ArrayList,因此您可以尝试使用 Row Table Model

【讨论】:

    【解决方案2】:

    将所有 Children 类添加到 JList 模型并定义自定义 ListCellRenderer。在渲染器中获取值并使用 Children 元素设置文本。

    【讨论】:

    • public void loadKids(ArrayList<Children> childs) { final String[] tmpData = new String[childs.size()]; for (Children ch : childs) { } listKids.setModel(new javax.swing.AbstractListModel() { // String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; @Override public int getSize() { return tmpData.length; } @Override public Object getElementAt(int i) { return tmpData[i]; } }); }
    • 这是我尝试在 jList 中加载 arrayList 的方式。没用。
    • 使用 DefaultListModel 传递您的子数组。简化 Children 的重写 toString() 方法以返回 name+"\t"+age
    猜你喜欢
    • 2021-09-07
    • 2014-06-18
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2016-11-28
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多