【问题标题】:Jtable swing get rows dataJtable swing 获取行数据
【发布时间】:2014-03-14 13:46:40
【问题描述】:

我在从 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);
    }    
}

问题是总是从表中获取除最后一行之外的所有数据,我不知道是什么。

【问题讨论】:

  • 应该是:username = TB_Accounts.getModel().getValueAt(i, 0); ?
  • 是的,我找到了,谢谢 :: ps : 如何设置线程解决??

标签: java swing jtable


【解决方案1】:

乍一看循环没有问题,但看起来第二行可能是罪魁祸首:

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

当您继续遍历行时,这可能会返回 null,然后导致循环通过 continue 调用跳过,如果用户名或密码为 null,则会发生这种情况。改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多