以编程方式选择JTable中的多个单元格

问题描述 投票:0回答:1

我有一个显示SQL结果的动态JTable。我正在实现一个搜索功能,并选择包含所搜索值的所有单元格。我正在使用JTable.changeSelection(row,col,true,false)。在JTable中,我实现了一个函数,该函数返回一个包含找到元素的行和列的对象的ArrayList。只要有最大值,这个工作正常。 5搜索结果,在此之后它选择某种方式随机。这是我的代码:

private void searchTable() {
    String pattern = JOptionPane.showInputDialog(scrollpane, "Pattern", "Find Text in Table", JOptionPane.QUESTION_MESSAGE);

    if (pattern != null) {

        Thread t = new Thread(new Runnable() {
            public void run() {
                busyCursor();
                ArrayList<TableFindResult> tfr = dt.search(pattern);
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        tableView.setColumnSelectionAllowed(true);
                        tableView.setRowSelectionAllowed(true);

                        tableView.clearSelection();
                        int a = 1;
                        Iterator iter = tfr.iterator();
                        while (iter.hasNext()) {
                            TableFindResult t = (TableFindResult) iter.next();                                
                            tableView.changeSelection(t.getRow(), t.getCol(), true, false);

                        }
                        defaultCursor();
                    }
                });
            }
        });
        t.start();

    } else {
        tableView.clearSelection();
    }

}

当结果少于5时,它就是这样的:

enter image description here

现在我的模式是'8',即使没有8个qazxsw poi也有很多外观

我已经调试验证,搜索功能,准备阵列正常工作。我的印象是JTable.changeSelection函数,它变得一团糟。但到目前为止我还没有找到解决方案。我认为选择heses cell是个好主意,因为我的JdbcTable能够转换选定的行,所以这两个函数会协调一致。有什么好主意吗?非常感谢

java swing jtable
1个回答
1
投票

具有cellSelectionEnabled的JTable采用所选行和所选列的交叉点。例如:如果选择单元格(r1,c1)和(r2,c2) - JTable选择(r1,c1),(r1,c2),(r2,c1)和(r2,c2)。

覆盖JTable中的enter image description here以获得您的预期行为。

在下面添加一个小例子:

isCellSelected
© www.soinside.com 2019 - 2024. All rights reserved.