高效地转换List<Object>;反对[][] [英] Efficienctly convert List&lt;Object&gt; to Object[][]

查看:23
本文介绍了高效地转换List<Object>;反对[][]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个学生对象列表,我需要将其转换为 Object[][] 以便我可以将数据附加到 GUI 中的 JTable 中,我有点不知道如何以高效的方式执行此操作/动态方式.

I currently have a List of Student objects that I need to convert to Object[][] so that I can append the data into my JTable in GUI, I'm kind of lost as to how to do it in an efficient/dynamic manner.

我没有什么要写在这里的,我可以给你数据的细节:

I don't have anything written down to post here, I can give you the details of the data:

List<Student> myStudents = new ArrayList<>();

需要以某种方式转变为:

Needs to somehow be morphed to:

Object[][] data = {{...}};

每个学生有 11 个要添加的字段,因此它会转到 Object[i][0 .. 10]

Each student has 11 fields to be added, so it would go to Object[i][0 .. 10]

所有这一切的替代方案,是以某种方式找到一种方法,将 List 传递到 new JTable(data, columnNames) 而不是 Object[][]

The alternative to all of this, is to somehow find a way to pass List<Student> inside new JTable(data, columnNames) instead of Object[][]

要么解决这个问题

推荐答案

所有这一切的替代方案,是以某种方式找到一种方法在 new JTable(data, columnNames) 中传递 List

The alternative to all of this, is to somehow find a way to pass List inside new JTable(data, columnNames)

这是更好的方法.您永远不应该尝试保留数据的两个副本.所有数据应该只存储在 TableModel 中.这意味着您需要创建一个自定义 TableModel 来保存 Student 对象.

This is the better approach. You should never try to keep two copies of the data. All the data should only be stored in the TableModel. This means you need to create a custom TableModel to hold the Student objects.

查看行表模型以获得基本实现可以支持任何自定义对象的 TableModel.您将需要实现一些方法来支持您的学生对象.JButtonTableModel 示例向您展示了如何做到这一点.

Check out Row Table Model for a basic implementation of a TableModel that can support any custom Object. You will need to implement a couple of methods to support your Student Object. The JButtonTableModel examples shows you how this can be done.

这篇关于高效地转换List<Object>;反对[][]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆