從邏輯上講,一個矢量是一個可調整大小的一維數組,所以我不認爲你可以將它轉換爲二維數組。
除了使用DefaultTableModel的,我想你會可以延長TableModel class或創建AbstractTableModel class的anonymous class,然後創建自己的表型,然後您可以用它來實例化你的表。
檢查TableDemo.java
.....
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
......
它解釋了這個實現相當不錯。
我嘗試了一種類似的方法,它確實有效,就像我現在要粘貼的東西,如果它要工作。讚賞。 – Jojo 2014-09-26 19:46:52