java 将数组列表数据添加到 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4562920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:58:45  来源:igfitidea点击:

add arraylist data to JTable

javaswingjtable

提问by Chamal

i hav a ArreayList which is contain PRIvariable(name of the class) class data. Below shows my part of the java code. So now i want to put this Arraylist data to Jtable. how can i do that. Here i already added pri.dateText , pri.sum , pri.countto ar(arraylist)

我有一个包含 PRIvariable(类名)类数据的 ArreayList。下面显示了我的部分 java 代码。所以现在我想把这个 Arraylist 数据放到 Jtable 中。我怎样才能做到这一点。在这里我已经添加pri.dateText , pri.sum , pri.countar(arraylist)

PRIvariable pri=new PRIvariable();
   while (reader.ready()) {
             String line = reader.readLine();
             String[] values = line.split(",");
             if(values[2].equals(pri.incDate)){
                 if(values[4].equals("KI")){
             pri.dateText=values[2]+"    "+values[4];
             pri.count=pri.count+1;
             pri.sum = pri.sum+Integer.parseInt(values[7]);
         }
             }
         }
     System.out.println(pri.dateText+"  "+pri.sum+" "+pri.count);
     ar.add(pri);

回答by Riduidel

Like all Swing components, a JTable relies upon the MVC pattern (at multiple levels, but that's not the subject).

与所有 Swing 组件一样,JTable 依赖于 MVC 模式(在多个级别,但这不是主题)。

You have one view (the JTable), one model (I'll come back on it later), and a controller (implemented here as a set of event listeners : one controller for each kind of control).

您有一个视图(JTable)、一个模型(稍后我会回来讨论它)和一个控制器(这里作为一组事件侦听器实现:每种控件一个控制器)。

The array you have could be a good model starting point. However, Swing provides far better way to inject your data in JTable. Indeed, a JTable uses as model an implementation of TableModel. Hopefully, there already exist an implementation : DefaultTableModel.

您拥有的数组可能是一个很好的模型起点。但是,Swing 提供了更好的方法将数据注入 JTable。实际上,JTable 使用TableModel. 希望已经存在一个实现:DefaultTableModel

So, here is what I suggest to you : create DefaultTableModel, put in its rows/columns all the data you want to display in your table, then call JTable#setModel(TableModel)to have thze table display your data.

所以,这是我给你的建议:创建 DefaultTableModel,将你想要在表中显示的所有数据放入它的行/列,然后调用JTable#setModel(TableModel)让表显示你的数据。

Obviously, you'll soon find various misfits between DefaultTableModel and what you want to do. It will then be time for you to create our very own table model. But that's another question.

显然,您很快就会发现 DefaultTableModel 与您想要做的事情之间存在各种不匹配。然后是时候创建我们自己的表模型了。但这是另一个问题。

Besides, don't forget to take a look at Swing tutorial, it's usually a good thing when dealing with Swing components.

此外,不要忘记看一看Swing 教程,在处理 Swing 组件时,这通常是一件好事。

回答by camickr

I don't see where you have an ArrayList anywhere.

我在任何地方都看不到 ArrayList 的位置。

First you create a PRIvariable object. Then you keep looping as you read a line of data from the file. For each line of data you split it into individual tokens and then add some of the token data to the PRIvariable ojbect. The problem is that you only have a single PRIVariable object. So every time you read a new line of data you change the values of the PRIvariable object. After all the looping you add this single PRIvariable object to your ArrayList, but you will only ever have one object in the ArrayList.

首先创建一个 PRIvariable 对象。然后在从文件中读取一行数据时继续循环。对于每一行数据,您将其拆分为单独的标记,然后将一些标记数据添加到 PRI 变量 ojbect。问题是您只有一个 PRIVariable 对象。因此,每次读取新数据行时,都会更改 PRIvariable 对象的值。在所有循环之后,您将这个单一的 PRIvariable 对象添加到您的 ArrayList,但您将永远只有一个对象在 ArrayList 中。

The easier solution is to update the TableModel as you get the data. Something like:

更简单的解决方案是在获取数据时更新 TableModel。就像是:

DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
...
...

while (reader.ready()) 
{              
    String line = reader.readLine();              
    String[] values = line.split(",");
    String[] row = new String[3];
    row[0] = values[?];
    row[1] = values[?];
    row[2] = values[?];
    model.addRow( row );
}

回答by I82Much

Look into GlazedLists. They make it extremely easy to create a suitable TableModel object from your list.

查看GlazedLists。它们使从列表中创建合适的 TableModel 对象变得非常容易。

ArrayList< PRIvariable> myList = new ArrayList<PRIvariable>();
... fill up the list ...
// This assumes your object has a getName() and getAge() methods
String[] propertyNames = {"name","age"};
String[] columnLabels = {"Name","Age"};
// Are these columns editable?
boolean[] writeable = {false, false};

EventList<PRIvariable> eventList = GlazedLists.eventList(myList);
EventTableModel<PRIvariable> tableModel = new EventTableModel<PRIvariable>(eventList,propertyNames,columnLabels,writable);
JTable table = new JTable(tableModel);