显示ArrayList表 [英] display a table of ArrayList
问题描述
我有一个 main.java
有一个按钮,当你按下它时,它会调用一个方法并返回一个节点的ArrayList;
我想在表中显示ArrayList(类节点中描述的5个字段)
如何做到这一点,问题是显示一些字段,因为它们是List类型?
I have a main.java
that has a button, when you press it, it calls a method and retuns an ArrayList of Nodes;
I want to display the ArrayList in a table ( 5 fields as described in class Node)
How to do that, The problem is to display some fields as they are List type?
Node.java
public class Node {
private String name;
private double value;
private List<Node> first;
private List<Node> second;
private List<Double> values;
//some methods...
}
main .java
main.java
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Red Bayesiana Visita a Asia ");
JPanel panel = new JPanel();
boton = new Button( "Get");
panel.add(boton);
frame.add(panel);
ArrayList<Node> arrayList;
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
arrayList = method("file.txt");
//insert into table arrayList of 5 fields?
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
推荐答案
As在 创建表格模型中讨论 a>,让节点
扩展 AbstractTableModel
并实现所需的方法。使用生成的模型创建 JTable
。
As discussed in Creating a Table Model, let Nodes
extend AbstractTableModel
and implement the required methods. Use the resulting model to create your JTable
.
附录:这是模型的概述。字段 name
和 value
可以使用默认渲染器,但你将决定如何渲染每个节点中找到的
。列表
Addendum: Here's an outline of the model. The fields name
and value
can use the default renderer, but you'll have decide how to render the List
s found in each Node
.
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/** @see http://stackoverflow.com/questions/5438516 */
public class Nodes extends AbstractTableModel {
private List<Node> nodes = new ArrayList<Node>();
@Override
public int getRowCount() {
return nodes.size();
}
@Override
public int getColumnCount() {
return 5; // A Node has five members
}
@Override
public Object getValueAt(int row, int col) {
Node node = nodes.get(row);
switch (col) {
case 0:
return node.name;
case 1:
return node.value;
case 2:
return node.first;
case 3:
return node.second;
case 4:
return node.values;
default:
return null;
}
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
private class Node {
private String name;
private double value;
private List<Node> first;
private List<Node> second;
private List<Double> values;
}
}
这篇关于显示ArrayList表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!