以编程方式在JTable中选择多个单元格 [英] Programmatically select multiple cells in a JTable

查看:61
本文介绍了以编程方式在JTable中选择多个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dynamic JTable displaying SQL-results. I'm implementing a search function and select all cells containing the value searched for. I'm using JTable.changeSelection(row,col,true,false). Within the JTable I have implemented a function that returns an ArrayList of an object containing row and col of the found elements. This works fine as long as there are max. 5 search results, after this it selects somehow random. This is my Code:

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个时的处理方法如下:

This is how it goes when less than 5 results:

现在我的模式是'8',即使没有数字8也有很多外观

now my pattern is '8', there are so many appereances even without the 8

我已调试以验证搜索功能(准备阵列)是否正常工作.我的印象是JTable.changeSelection-function变得一团糟.但是到目前为止,我还没有找到解决方案. 我认为选择heses单元是一个好主意,因为我的JdbcTable能够转置选定的行,因此这两个函数可以协调. 有什么想法吗? 非常感谢

I have debugged to verify, that the searchfunction, preparing the array works correctly. I have the impression it is the JTable.changeSelection-function, that gets a mess. But I did not find a solution up to now. I think to select heses cells is a good idea, because my JdbcTable is able to transpose selected rows so these two functions would harmonize. Any idea out there? Many thanks

推荐答案

带有cellSelectionEnabled的JTable接受所选行和所选列的交集.例如:如果选择单元格(r1,c1)和(r2,c2)-JTable选择(r1,c1),(r1,c2),(r2,c1)和(r2,c2).

JTable with cellSelectionEnabled, takes the intersections of rows selected and columns selected. For example: if you select cells (r1, c1) and (r2, c2) - JTable selects (r1,c1), (r1, c2), (r2, c1) and (r2, c2).

在JTable中重写isCellSelected以获得预期的行为.

Override isCellSelected in JTable to get your intended behavior.

在下面添加一个小示例:

Adding a small example below:

package tableselection;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableSelection extends JFrame {
    private static final long serialVersionUID = 1L;
    String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
    Object[][] data = { { "Kathy", "Smith", "Snowboarding", Integer.valueOf(5), Boolean.valueOf(false) },
            { "John", "Doe", "Rowing", Integer.valueOf(3), Boolean.valueOf(true) },
            { "Sue", "Black", "Knitting", Integer.valueOf(2), Boolean.valueOf(false) },
            { "Jane", "White", "Speed reading", Integer.valueOf(20), Boolean.valueOf(true) },
            { "Joe", "Brown", "Pool", Integer.valueOf(10), Boolean.valueOf(false) } };

    public TableSelection() {
        JPanel main = new JPanel();
        JTable table = new JTable(data, columnNames) {
            private static final long serialVersionUID = 1L;
            List<Point> selected = new ArrayList<Point>();
            @Override protected void processMouseEvent(MouseEvent e) {
                if(e.getID() != MouseEvent.MOUSE_PRESSED)
                    return;
                int row = ((JTable)e.getSource()).rowAtPoint(e.getPoint());
                int col = ((JTable)e.getSource()).columnAtPoint(e.getPoint());
                if(row >= 0 && col >= 0) {
                    Point p = new Point(row, col);
                    if(selected.contains(p))
                        selected.remove(p);
                    else
                        selected.add(p);
                }
                ((JTable)e.getSource()).repaint();
            }

            @Override public boolean isCellSelected(int arg0, int arg1) {
                return selected.contains(new Point(arg0, arg1));
            }
        };
        JScrollPane pane = new JScrollPane(table);
        main.add(pane);
        this.add(main);

        this.setSize(800, 600);
        this.setVisible(true);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TableSelection();
    }

}

这篇关于以编程方式在JTable中选择多个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆