【问题标题】:Select a JTable row选择 JTable 行
【发布时间】:2018-10-30 14:11:39
【问题描述】:

我试图从 GUI 中通过单击从 JTable 中选择我的行。我选择得很好,但是当我尝试删除某些内容时,我得到 java.lang.ArrayIndexOutOfBoundsException: 2>=1

    if(row >=  0) 
    {
    String id = table_1.getModel().getValueAt(row, 0).toString(); // here I got the error
    int idd = Integer.parseInt(id);
    Account account = bank.getAccounts().get(idd);
    bank.removeAccount(account);
    }

我该如何解决??

【问题讨论】:

  • 是什么决定了变量row的值?它应该类似于:row = table_1.getSelectedRow();.

标签: java jtable indexoutofboundsexception


【解决方案1】:

将所有内容都放入 try and catch 中,并确保变量 Row 获得所需的值。我遇到了类似的问题,我所要做的就是在不同的线程上处理数据,而 AWT 线程应该只在 GUI 元素上工作。并且永远不要从除 AWT 线程之外的任何线程更新 GUI 元素。如果您从数据库中获取这些值,则使用不同的线程从数据库中删除,然后使用 AWT 线程相应地重新绘制您的 GUI。

解决这个问题的一个简单方法是,当您尝试多次删除时,IndexOutOfBounds 异常会更改其值,例如,如果一次是 2>=1,第二次是 3>=2 或其他情况否则,如果它不断变化,您需要更好地处理线程。希望这可以帮助。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多