java 使用 ArrayList 创建 jTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26973577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create jTable with ArrayList
提问by Kyo Akashi
I searched for a while but I cant find a solution for my problem.
我搜索了一段时间,但找不到解决我的问题的方法。
I want to display the Values of an ArrayList in a JTable, i'm pretty new to this and cant fix the error.
我想在 JTable 中显示 ArrayList 的值,我对此很陌生,无法修复错误。
package Statistik;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Statistic {
private static ArrayList<String> rowA = new ArrayList();
private static ArrayList<String> rowB = new ArrayList();
private static ArrayList<String> rowC = new ArrayList();
private static ArrayList<String> titel = new ArrayList();
private static ArrayList<Object> table = new ArrayList();
public static void main(String[] args) {
titel.add("Name");
titel.add("Art der Bearbeitung");
titel.add("Datum");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
table.add(rowA);
table.add(rowB);
table.add(rowC);
// Das JTable initialisieren
JTable EndTable = new JTable( table , titel );
JFrame frame = new JFrame( "Demo" );
frame.getContentPane().add(new JScrollPane( EndTable ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
public static void addRows(String rowa, String rowb, String rowc) {
rowA.add(rowa);
rowB.add(rowb);
rowC.add(rowc);
}
}
I can't set the ArrayList
table
as first Value in my EndTable
, but i dont know how I should do otherwise.
我无法ArrayList
table
在我的 中设置第一个值EndTable
,但我不知道我应该怎么做。
Thank you all for trying to answer my problem.
感谢大家尝试回答我的问题。
Edit
编辑
My goal is to make a List with
我的目标是制作一个列表
Entity-Name, art of change, Date
实体名称,变化的艺术,日期
so I thought it would be the best to use an ArrayList because it's flexible. It have to be flexible because we dont know how much the user will change.
所以我认为最好使用 ArrayList,因为它很灵活。它必须灵活,因为我们不知道用户会改变多少。
回答by Naruto Biju Mode
You should create custom TableModel for your table see the following link for more details Custom JTable Model
您应该为您的表创建自定义 TableModel,请参阅以下链接以获取更多详细信息自定义 JTable 模型
Here's some code to start with:
下面是一些开始的代码:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Test extends JFrame {
public Test() {
setBounds(100, 100, 500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new ModelData());
add(new JScrollPane(table));
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
class ModelData extends AbstractTableModel {
List<Data> data = new ArrayList<Data>();
String colNames[] = { "Name", "Type", "Date" };
Class<?> colClasses[] = { String.class, String.class, Date.class };
ModelData() {
data.add(new Data("name 1", "type 1", new Date()));
data.add(new Data("name 2", "type 2", new Date()));
data.add(new Data("name 3", "type 3", new Date()));
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return colNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return data.get(rowIndex).getName();
}
if (columnIndex == 1) {
return data.get(rowIndex).getType();
}
if (columnIndex == 2) {
return data.get(rowIndex).getDate();
}
return null;
}
public String getColumnName(int columnIndex) {
return colNames[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return colClasses[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
data.get(rowIndex).setName((String) aValue);
}
if (columnIndex == 1) {
data.get(rowIndex).setType((String) aValue);
}
if (columnIndex == 2) {
data.get(rowIndex).setDate((Date) aValue);
}
fireTableCellUpdated(rowIndex, columnIndex);
}
}
class Data {
String name;
String type;
Date date;
public Data(String name, String type, Date date) {
super();
this.name = name;
this.type = type;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
And this is the result:
这是结果:
回答by Isthatso
Nope since , JTable
has no reserved argument for ArrayList
, but a trick will solve it ! you know that the arguments for JTable
is also (Object[][], object[])
不,因为JTable
没有保留的论点ArrayList
,但是一个技巧会解决它!你知道的论据JTable
也是(Object[][], object[])
Object[] tempTitel = titel.toArray(); // return Object[]
String[][] tempTable = new String[table.size()][];
int i = 0;
for (List<String> next : table) {
tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
}
JTable EndTable = new JTable(tempTable,tempTitel);
Note that I change ArrayList<String> table= new ArrayList()
to ArrayList<ArrayList<String>> table = new ArrayList();
so when combine it :
请注意,我
在组合它时更改ArrayList<String> table= new ArrayList()
为ArrayList<ArrayList<String>> table = new ArrayList();
so :
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Statistic {
private static ArrayList<String> rowA = new ArrayList();
private static ArrayList<String> rowB = new ArrayList();
private static ArrayList<String> rowC = new ArrayList();
private static ArrayList<String> titel = new ArrayList();
private static ArrayList<ArrayList<String>> table = new ArrayList();
public static void main(String[] args) {
titel.add("Name");
titel.add("Art der Bearbeitung");
titel.add("Datum");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
addRows("buchung", "Created", "10.10.10");
table.add(rowA);
table.add(rowB);
table.add(rowC);
Object[] tempTitel = titel.toArray();
String[][] tempTable = new String[table.size()][];
int i = 0;
for (List<String> next : table) {
tempTable[i++] = next.toArray(new String[next.size()]);
}
JTable EndTable = new JTable(tempTable,tempTitel);
JFrame frame = new JFrame("Demo");
frame.getContentPane().add(new JScrollPane(EndTable));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void addRows(String rowa, String rowb, String rowc) {
rowA.add(rowa);
rowB.add(rowb);
rowC.add(rowc);
}
}
回答by Kyo Akashi
Perhaps you should post your error.
I can see from JTable
constructor in the Java API that it does not take an ArrayList
as arguments.
也许你应该发布你的错误。我可以从JTable
Java API 中的构造函数中看到它不接受 anArrayList
作为参数。
JTable(Object[][] rowData, Object[] columnNames) Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.
JTable(Object[][] rowData, Object[] columnNames) 构造一个 JTable 来显示二维数组 rowData 中的值,列名 columnNames。
Perhaps you should try using primitive arrays
?
也许你应该尝试使用原始的arrays
?