将arraylist数据添加到JTable [英] add arraylist data to JTable

查看:530
本文介绍了将arraylist数据添加到JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

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

你有一个视图(JTable),一个模型(我稍后再回来),和控制器(在这里实现为一组事件监听器:每种控件都有一个控制器)。

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

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

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.

所以,我建议你这样做:创建DefaultTableModel,放入其行/列要在表中显示的所有数据,然后调用 JTable#setModel(TableModel) 让thze表显示您的数据。

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和你想做的事情之间存在各种不匹配。然后,您将有时间创建我们自己的表模型。但这是另一个问题。

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.

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

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

这篇关于将arraylist数据添加到JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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