使用 ArrayList 创建 jTable [英] Create jTable with ArrayList
问题描述
我搜索了一段时间,但找不到解决问题的方法.
I searched for a while but I cant find a solution for my problem.
我想在 JTable 中显示 ArrayList 的值,我对此很陌生,无法修复错误.
I want to display the Values of an ArrayList in a JTable, i'm pretty new to this and cant fix the error.
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);
}
}
我无法将 ArrayList
table
设置为我的 EndTable
中的第一个值,但我不知道我应该怎么做.
I can't set the ArrayList
table
as first Value in my EndTable
, but i dont know how I should do otherwise.
感谢大家尝试回答我的问题.
Thank you all for trying to answer my problem.
编辑
我的目标是制作一个列表
My goal is to make a List with
Entity-Name、变化的艺术、日期
Entity-Name, art of change, Date
所以我认为最好使用 ArrayList,因为它很灵活.它必须灵活,因为我们不知道用户会改变多少.
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.
推荐答案
不,因为 JTable
没有 ArrayList
的保留参数,但是一个技巧可以解决它!你知道 JTable
的参数也是 (Object[][], object[])
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[])
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);
请注意,我更改了 ArrayList
到 ArrayList
所以当结合它时:
Note that I change ArrayList<String> table= new ArrayList()
to ArrayList<ArrayList<String>> table = new ArrayList();
so when combine it :
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);
}
}
这篇关于使用 ArrayList 创建 jTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!