我有一个main.java
有一个按钮,当你按下它时,它会调用一个方法并返回一个节点的ArrayList;
我想在表中显示ArrayList(类节点中描述的5个字段)
怎么做,问题是显示一些字段,因为它们是列表类型?
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
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);
}
}
答案 0 :(得分:4)
正如Creating a Table Model中所述,让Nodes
扩展AbstractTableModel
并实施所需的方法。使用生成的模型创建JTable
。
附录:这是模型的概述。字段name
和value
可以使用默认renderer,但您已决定如何render List
找到每个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;
}
}
}。
{{1}}