0

所以这就是问题所在。我有一个由 3 个组合框、一个文本字段、几个按钮和一个 JTable 组成的 JDialog 框。JTable 信息根据文本字段和组合框进行过滤,因此例如它以所有数据开头,然后缩小为仅以用户决定的任何字符串值开头的数据。

但是发生的情况是,虽然值过滤正确,但如果我单击 JTable(在没有行的空白区域中),那么被删除的行会显示出来,就像在我单击它们之前它们是不可见的一样。我几乎试过了一切:每次单击过滤器时,我都尝试重新创建表(甚至不起作用的坏技巧),我已经调用了所有的重绘、重新验证、更改方法,我从头开始重写了对话框以确保我没有犯任何愚蠢的错误(如果我犯了一个,至少我没有找到它),并且我尝试将它们放在不同的线程上。我没有尝试过的唯一解决方法是使用摇摆工人,但那是因为我的过滤有点太复杂,我无法弄清楚什么去哪里以及如何正确扩展摇摆工人。GUI 是由 netbeans (bleh) 生成的,并且在我的其他十几个 JDialogs 中工作得很好(实际上是完美的)。这是过滤的方法,如果您能提供帮助,将不胜感激。

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        nameFilter = "task1";
        javax.swing.table.DefaultTableModel dm = (javax.swing.table.DefaultTableModel)jTable1.getModel();


       tempParameters = parameters;
       String currentString;
       int rowNumber = 0;
       while (dm.getRowCount()>rowNumber){
           currentString = (String)(jTable1.getValueAt(rowNumber,1));
           if(!nameFilter.equalsIgnoreCase(currentString.substring(0,nameFilter.length()))){
               dm.removeRow(rowNumber);
               parameters--;
           }
           else rowNumber++;
       }
       parameters = numOfRows;
}

更新,我还从下面的评论中实现了过滤器,虽然它过滤掉了正确的数据,但它有完全相同的问题。不过,将来我可能会使用此过滤器功能,所以谢谢。

另一个更新,即使删除了除此块之外的所有内容,代码仍然失败,并且所有(至少我相信..)我在这里做的是做一个简单的删除行调用。希望这个对你有帮助。

4

3 回答 3

0

Couple of observations:

  • If the filter happens to be larger than the string content of the row, it'll throw in the substring call
  • Calling the dm.removerow is generating a bunch of tablerowsdeleted events.
  • You're asking for a rowcount from the model, yet are getting the value through the table (a little inconsistent, if the model gets wrapped around another model you might be acting upon different rows), so instead of jtable1.getvalueat, use the dm.getvalueat.

    I think what might be happening is that as the events get fired I see there are repaint and revalidate events fired in the JTable, these can be trampling over each other as they get enqueued in the EDT.

    What I would suggest is to create a new datamodel, add the rows that you want to keep, and then reassign it to your jTable1.setModel(newDm);

    Also to watch for is if someone else is modifying the model while you're in your eventlistener.

    Hope this helps

  • 于 2011-06-23T18:15:38.533 回答
    0

    您是否尝试过每次要过滤时都创建一个新模型,而不是通过删除行来清除它?创建新模型,将相关行复制到新模型,在表中设置新模型。真的不应该是必要的,但它可能是一个快速修复。

    另外,我真的想知道为什么在使用 equalsIgnoreCase 比较它们时,为什么要在两个字符串上调用 toLowerCase。

    于 2011-06-23T16:35:20.093 回答
    0

    只要从 EDT 调用此方法,我认为不会有线程问题。尝试使用

    SwingUtilties.isEventDispatchThread()
    

    确保;确定。

    如果您查看 DefaultTableModel 的 API,则会将更新发送到您的 JTable,该 JTable 将自行重新绘制,所以我认为这不是问题。

    我猜这是一个逻辑问题。如果您可以将逻辑提取到单独的方法中,则将更容易测试和验证它是否按照您的预期更新模型。

    于 2011-06-23T16:58:42.770 回答