简体   繁体   中英

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. The JTable appears with only the first row of values from my database, and they all show up in the same column at once. 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)

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) should probably be model.addRow(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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