将向量行插入到 JTable 中?
Inserting vector rows into a JTable?
我正在尝试将矢量中的项目添加到我的 JTable 中,但似乎无法正确完成。 JTable 只显示了我数据库中的第一行值,并且它们都同时显示在同一列中。当我将它们插入 table 时如何分隔各个列值?以及如何一次将多行值放入 table?
connect = DriverManager.getConnection(url,user,pass);
System.out.println("Connected to database.\n");
statement = connect.createStatement();
ResultSet result = statement.executeQuery("SELECT * FROM aboyer_tickets_1");
//get values for table
System.out.println("Adding data to table model...\n");
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (result.next()) {//model
Vector<Object> row = new Vector<Object>();//row
for (int columnIndex = 1; columnIndex <= 6; columnIndex++) {
row.add(result.getObject(columnIndex));
}
System.out.println(row + "\n");
data.add(row);
model.addRow(data);
}
System.out.println("Done.\n");
控制台输出:
已连接到数据库。
Adding data to table model...
[1, 531961, user1, M, PH 109 Computer wont turn on, O]
[2, 502492, user1, H, wifi connectivity issue, O]
[3, 469432, admin, L, mouse replacement - MSV, O]
[4, 140627, user1, H, Lost login for WH 121 computer, O]
Done.
table 中的输出(尽我所能):
1 | 2 | 3 | 4 | 5 | 6
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
所以,基于 JavaDocs for DefaultTableModel#addRow(Vector)
Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
model.addRow(data)
应该是 model.addRow(row)
我正在尝试将矢量中的项目添加到我的 JTable 中,但似乎无法正确完成。 JTable 只显示了我数据库中的第一行值,并且它们都同时显示在同一列中。当我将它们插入 table 时如何分隔各个列值?以及如何一次将多行值放入 table?
connect = DriverManager.getConnection(url,user,pass);
System.out.println("Connected to database.\n");
statement = connect.createStatement();
ResultSet result = statement.executeQuery("SELECT * FROM aboyer_tickets_1");
//get values for table
System.out.println("Adding data to table model...\n");
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (result.next()) {//model
Vector<Object> row = new Vector<Object>();//row
for (int columnIndex = 1; columnIndex <= 6; columnIndex++) {
row.add(result.getObject(columnIndex));
}
System.out.println(row + "\n");
data.add(row);
model.addRow(data);
}
System.out.println("Done.\n");
控制台输出: 已连接到数据库。
Adding data to table model...
[1, 531961, user1, M, PH 109 Computer wont turn on, O]
[2, 502492, user1, H, wifi connectivity issue, O]
[3, 469432, admin, L, mouse replacement - MSV, O]
[4, 140627, user1, H, Lost login for WH 121 computer, O]
Done.
table 中的输出(尽我所能):
1 | 2 | 3 | 4 | 5 | 6
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
所以,基于 JavaDocs for DefaultTableModel#addRow(Vector)
Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
model.addRow(data)
应该是 model.addRow(row)