简体   繁体   中英

Jtable swing get rows data

i have a little problems with getting rows data from a jTable :

for (int i = 0; i < TB_Accounts.getRowCount(); i++) {
    username = TB_Accounts.getModel().getValueAt(i, i);
    password = TB_Accounts.getModel().getValueAt(i, 1);

    if (username == null || password == null) {
        continue;
    }

    System.out.println("userName : " + username);
    System.out.println("password : " + password);

    if (!username.toString().equalsIgnoreCase("") && !password.toString().equalsIgnoreCase((""))) {
        accouts.add(new Account(username.toString(), password.toString()));

        System.out.println("in :: userName : " + username);
        System.out.println("in :: password : " + password);
    }    
}

the problem is that a always get all data from the table except the last row, i dont know wht .

Nothing wrong with the loop on first glance but looks like line two could be the culprit:

...
username = TB_Accounts.getModel().getValueAt(i, i);
...

That is probably returning null as you proceed through the rows and subsequently causing the loop to skip via the continue call which occurs if username or password are null. Change to:

...
username = TB_Accounts.getModel().getValueAt(i,0);
...

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