我有一個arraylist,arrayList的每個元素都包含一個小數組arrayList。在JTable中顯示arrayLists的arrayList
我需要從這些數據創建一個表。你會推薦什麼來解決這個問題?
預先感謝任何幫助
我有一個arraylist,arrayList的每個元素都包含一個小數組arrayList。在JTable中顯示arrayLists的arrayList
我需要從這些數據創建一個表。你會推薦什麼來解決這個問題?
預先感謝任何幫助
要顯示一個列表的列表的GUI表,我會創建一個實現「TableModel的接口,並有一個構造函數在一個類的列表>」
public class ListTableModel <T> implements TableModel {
private List<List<T>> source;
public ListTableModel(List<List<T>> source) {
this.source = source;
}
//Override 'getRowCount'
//The row count would be calculated as the size of the outer list.
@Override
public int getRowCount() {
return source.size();
}
//Override 'getColumnCount'
//The column count would be calculated as the max size of the inner lists
@Override
public int getColumnCount() {
int max = 0;
for(List<T> row : source) {
max = Math.max(max, row.size());
}
return max;
}
//Override 'getColumnName'
//Lets go ahead and just give a unique name to each column based on the index.
//This could be populated from an input taken by the constructor, but we
//won't worry about that now.
@Override
public String getColumnName(int columnIndex) {
return "Column " + columnIndex;
}
//Override 'getColumnClass'
//The class would technically be the generic type 'T', so to get this we
//will simply just get the calss of the first element.
@Override
public Class<?> getColumnClass(int columnIndex) {
return source.get(0).get(0).getClass();
}
//Override 'isCellEditable'
//I'm going to assume we don't want cell to be editable.
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
List<T> row = source.get(rowIndex);
if(columnIndex >= row.size())
return null;
else
return row.get(columnIndex);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//required but we will assume that you cannot change the source list
//if we needed to, it wouldn't be too difficult to implement.
}
@Override
public void addTableModelListener(TableModelListener l) {
//required but not used (will only be used if the source could change)
}
@Override
public void removeTableModelListener(TableModelListener l) {
//required but not used (will only be used if the source could change)
}
}
你會使用這個模型與您的清單,像這樣:
List<List<String>> myList = new ArrayList();
//Populate 'myList'...
JTable table = new JTable();
//Add table to view...
table.setModel(new ListTableModel(myList));
編輯我可以告訴你如何實現TableModel的,如果你想。
如果您可以給我們一個關於如何實現TableModel的快速教程,那將會很棒!非常感謝。 – Hoggie1790
完美!非常感謝,我會放手一搏。我真的很欣賞幫助 – Hoggie1790
@ user2029412我改變了代碼來實現TableModel。這是一個非常基本的實現,我會建議看看Che創建表模型的例子。順便說一句,創建這樣一個類的好處是,您可以在任何項目中重用它! – souldzin
一個小例子展示瞭如何創建模型和你的對象存儲在模型中。對數據的任何操作都應該使用模型完成。
創建一個模型並使用一個列表來存儲該表的數據。如下面的例子所示。表中的每一行都是一個Student
對象,每個對象都存儲在列表中。遍歷列表並獲取每個對象並顯示在表中。這是使用getValueAt(..)
方法完成的。
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class JTableList {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
CustomModel model = new CustomModel();
JTable table = new JTable();
table.setModel(model);
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
class CustomModel extends AbstractTableModel {
List<Student> data;
String[] columnNames = {"Name", "Age"};
public CustomModel() {
data = new ArrayList<Student>();
data.add(new Student("Amar", 1));
data.add(new Student("Sam", 2));
data.add(new Student("Amar", 1));
data.add(new Student("Sam", 2));
data.add(new Student("Amar", 1));
data.add(new Student("Sam", 2));
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = data.get(rowIndex);
switch (columnIndex) {
case 0:
return student.getName();
case 1:
return student.getAge();
default:
return null;
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
的List Table Model可用來做到這一點。
這是一個更復雜,更靈活的解決方案,實現了此主題中給出的早期建議。
是否要顯示文本表或GUI表? – souldzin
圖形用戶界面表,謝謝 – Hoggie1790
內部列表包含什麼內容?它是否包含對象? – Amarnath