简体   繁体   English

将向量行插入到JTable中?

[英]Inserting vector rows into a JTable?

I'm trying to add items from a vector into my JTable and I can't seem to get it right. 我试图将向量中的项目添加到我的JTable中,但似乎无法正确完成。 The JTable appears with only the first row of values from my database, and they all show up in the same column at once. JTable仅显示数据库中第一行的值,并且它们一次都显示在同一列中。 How do I separate the the individual column values when I insert them into the table? 将单独的列值插入表格时,如何分隔它们? And how do I put more than one row of values into the table at once? 以及如何一次将多于一行的值放入表中?

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");

Output in console: Connected to database. 控制台中的输出:已连接到数据库。

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.

Output in the table (as best as I can draw it): 表中的输出(尽我所能绘制):

             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]

So, based on the JavaDocs for DefaultTableModel#addRow(Vector) 因此,基于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. 除非指定rowData,否则新行将包含空值。 Notification of the row being added will be generated. 将生成有关添加行的通知。

model.addRow(data) should probably be model.addRow(row) model.addRow(data)可能应该是model.addRow(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM