将 arraylist 数据添加到 JTable

发布于 2024-10-09 14:16:38 字数 737 浏览 0 评论 0原文

我有一个 ArreayList,其中包含 PRIvariable(类名称)类数据。下面展示了我的部分java代码。所以现在我想把这个Arraylist数据放入Jtable中。我怎样才能做到这一点。这里我已经将 pri.dateText 、 pri.sum 、 pri.count 添加到 ar(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);

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.count to ar(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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹泡泡o 2024-10-16 14:16:38

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

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

您拥有的数组可能是一个很好的模型起点。然而,Swing 提供了更好的方法将数据注入到 JTable 中。事实上,JTable 使用 < 的实现作为模型代码>TableModel。希望已经存在一个实现: 默认表模型

因此,我建议您:创建 DefaultTableModel,将要在表中显示的所有数据放入其行/列中,然后调用 JTable#setModel(TableModel) 具有该表显示您的数据。

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

另外,别忘了看看 Swing 教程 ,在处理 Swing 组件时这通常是一件好事。

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

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).

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.

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.

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.

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

我很OK 2024-10-16 14:16:38

我看不到你在哪里有 ArrayList 。

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

更简单的解决方案是在获取数据时更新 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 );
}

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

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.

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

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 );
}
空名 2024-10-16 14:16:38

查看 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);

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文