将 arraylist 数据添加到 JTable
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与所有 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.
我看不到你在哪里有 ArrayList 。
首先创建一个 PRIvariable 对象。然后,当您从文件中读取一行数据时,将继续循环。对于每一行数据,您将其拆分为单独的标记,然后将一些标记数据添加到 PRIvariable 对象中。问题是您只有一个 PRIVariable 对象。因此,每次读取新行数据时,都会更改 PRIvariable 对象的值。在所有循环之后,您将这一单个 PRIvariable 对象添加到 ArrayList 中,但 ArrayList 中只会有一个对象。
更简单的解决方案是在获取数据时更新 TableModel。像这样的东西:
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:
查看 GlazedLists。它们使得从列表中创建合适的 TableModel 对象变得非常容易。
Look into GlazedLists. They make it extremely easy to create a suitable TableModel object from your list.