从逻辑上讲,一个矢量是一个可调整大小的一维数组,所以我不认为你可以将它转换为二维数组。
除了使用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