我仍在尝试理解Java编程并遇到了这个挑战,我有一个表,并希望通过此DefaultTableModel
将数据加载到其中private Vector <Object> Headings =new Vector <Object>();
private Vector <Object> Data = new Vector <Object> ();
Headings = acquire.Account("StudentHeader");
Data = acquire.Account("StudentData");
Object[] ids = Headings.toArray();
//Object[][] out = to2DimArray(Data);
DefaultTableModel model = new DefaultTableModel(out, ids);
根据需要,“out”是二维数组。问题是我不知道如何将我的“数据”传输/更改为表所需的二维数组。我尝试使用DefaultTableModel的Vector形式,但是有一个错误,所以我决定使用加载标题的数组形式。 你能告诉我如何将矢量转换成二维数组,这样我可以加载我的表吗?
答案 0 :(得分:0)
逻辑上说,一个矢量是一个可调整大小的一维数组,所以我不认为你可以把它转换成一个二维数组。
我认为您可以扩展DefaultTableModel或创建TableModel class的anonymous class,而不是使用AbstractTableModel class,然后创建自己的表模型然后,您可以使用它来实例化您的表。
.....
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();
}
...
它很好地解释了这个实现。
答案 1 :(得分:0)
您的Data对象是一维的Vector,因此您无法将其直接转换为二维对象。
尝试将数据构建为Vector<Vector<Object>>
,其中每个元素都是表格的一行。
这样的事情:
Vector <Object> headings = new Vector <Object>();
Vector <Vector<Object>> data = new Vector <Vector<Object>> ();
headings = acquire.Account("StudentHeader");
for(int i = 0 ; i < yourStudentDataNumber ; i++) {
Vector <Object> dataElement = new Vector <Object>();
dataElement = acquire.Account("StudentHeader");
//i don't know what is acquire object, this for loop is only an example
// however you need to build a Vector<Object> for each row of the table
data.add(dataElement);
}
Object[] ids = headings.toArray();
int numberOfRow = data.size();
Object[][] bidimArray = new Object[numberOfRow][];
for(int i = 0 ; i < numberOfRow ; i++) {
bidimArray[i] = data.get(i).toArray();
}
DefaultTableModel model = new DefaultTableModel(bidimArray, ids);
答案 2 :(得分:0)
好吧,这就是我为此做出的努力,它起作用了:
Vector <Object> Headings =new Vector <Object>();
Vector <Object> Data = new Vector <Object> ();
Vector <Object> Extract = new Vector <Object> ();
Headings = acquire.Account("IncomeHeader");
Data = acquire.Account("IncomeData");
int DataSize = Data.size(); int colum = Headings.size();
int round = DataSize/colum; int cover = DataSize; int t =1;
while(t<=round){
Extract = getVector(Data, colum);
model.addRow(Extract);
t++;
}
private Vector<Object> getVector(Vector<Object> data, int colum) {
Vector vector = new Vector(); int h = data.size(); int r = 0;
while(h>s){
r++;
vector.add(data.get(s));
s++;
if(r==colum){
System.out.println("vector: "+vector);
return vector;
}
}
return null;
}