Reputation: 223
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.
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.
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.
Upvotes: 2
Views: 16849
Reputation: 3099
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);
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);
}
}
Upvotes: 3
Reputation: 2091
You should create custom TableModel for your table see the following link for more details Custom JTable Model
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:
Upvotes: 5
Reputation:
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(Object[][] rowData, Object[] columnNames) Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.
Perhaps you should try using primitive arrays
?
Upvotes: 0