失去对 JTable 单元格的选择

Lose selection of a JTable cell

当您单击一个 JTable 单元格时,该行变为 "selected",我想要它以便当我单击其他任何内容时,它变为未选中状态。

我正在考虑在 table 上使用鼠标侦听器来执行此操作,但不确定如何识别(不单击 table)。有什么想法吗?

这就是我正在尝试的:

 jTable.addMouseListener(new MouseAdapter(){
                @Override
                public void mouseClicked(MouseEvent e){
                    System.out.println("click");}});

但它只在我点击第一列时打印 click 而当我点击不是 table 的东西时肯定不会。

当我识别出该事件时,我会调用此方法:

public void loseCellFocus()
{
    jTable.getCellEditor().stopCellEditing();
    jTable.clearSelection();
}

使用附加到 JTableFocusListener,当焦点离开 table 时,这会告诉您。

有关详细信息,请参阅 How to Write a Focus Listener

例如...

    table.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            loseCellFocus();
        }           
    });

当然,只有当键盘焦点转移到能够接收键盘焦点的新组件时,这才会起作用

This causes my loseCellFocus() method to be called right away, as soon as I click on any cell it's called

您可以使用 JTable#setSurrendersFocusOnKeystroke 或检查焦点转移到的组件是否是 JTable

的子组件

例如...

table.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
        if (e.getOppositeComponent().getParent() != table) {
            loseCellFocus();
        }
    }
});

可运行示例...

好吧,这变得混乱了。我不仅必须向 JTable 添加一个 FocusListener,而且我必须确保将一个添加到 TableCellEditor 组件:P

这只是一个概念证明,我有专门的 类 能够通过一些通用接口引发事件或触发所需的功能

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            JTable table = new JTable(new DefaultTableModel(10, 10));

            JTextField editorField = new JTextField(10);
            editorField.setBorder(new EmptyBorder(1, 1, 1, 1));
            editorField.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    TableCellEditor cellEditor = table.getCellEditor();
                    if (cellEditor != null) {
                        if (!cellEditor.stopCellEditing()) {
                            cellEditor.cancelCellEditing();
                        }
                    }

                    Component gotFocus = e.getOppositeComponent();
                    if (!gotFocus.equals(table)) {
                        table.clearSelection();
                    }
                }
            });
            DefaultCellEditor editor = new DefaultCellEditor(editorField);
            table.setDefaultEditor(Object.class, editor);

            add(new JScrollPane(table));
            table.addFocusListener(new FocusAdapter() {

                @Override
                public void focusLost(FocusEvent e) {
                    Component gotFocus = e.getOppositeComponent();
                    if (!gotFocus.getParent().equals(table)) {
                        TableCellEditor cellEditor = table.getCellEditor();
                        if (cellEditor != null) {
                            if (!cellEditor.stopCellEditing()) {
                                cellEditor.cancelCellEditing();
                            }
                        }
                        table.clearSelection();
                    }
                }

            });

            JTextField field = new JTextField(10);
            add(field, BorderLayout.SOUTH);
        }

    }

}

AWTEventListener 示例...

好吧,我真的不想走这条路,因为它最终会陷入混乱的交叉条件,但是。基本上这会监视所有 MouseEventFocusEvents,它会执行一些向后的 summersults 以测试有效条件(因为我们需要确保不仅 JTable 是事件的一部分,但如果编辑器是事件的一部分)并根据这些结果,停止单元格编辑并清除选择...

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JTable table = new JTable(new DefaultTableModel(5, 5));
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(20, 20, 20, 20));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JScrollPane(table), gbc);
            add(new JTextField(10), gbc);

            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                @Override
                public void eventDispatched(AWTEvent event) {
                    if (event instanceof FocusEvent) {
                        FocusEvent focusEvent = (FocusEvent) event;
                        if (focusEvent.getID() == FocusEvent.FOCUS_LOST) {
                            Component focusTo = focusEvent.getOppositeComponent();
                            Component focusFrom = focusEvent.getComponent();

                            JTable table = getTableFrom(focusFrom);
                            if (focusTo == null || !focusTo.getParent().equals(table)) {

                                stopCellEditing(table);
                                clearSelection(table);

                            }
                        }
                    } else if (event instanceof MouseEvent) {
                        MouseEvent mouseEvent = (MouseEvent) event;
                        if (mouseEvent.getID() == MouseEvent.MOUSE_CLICKED) {
                            Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                            JTable table = getTableFrom(focusOwner);
                            System.out.println("     table = " + table);
                            System.out.println("focusOwner = " + focusOwner);
//                          if ((table != null && mouseEvent.getComponent() != table) || (focusOwner != null && !focusOwner.getParent().equals(table))) {
                            if ((table != null && mouseEvent.getComponent() != table) && (focusOwner != null && !focusOwner.getParent().equals(table))) {
                                stopCellEditing(table);
                                clearSelection(table);
                            }
                        }
                    }
                }

                protected JTable getTableFrom(Component component) {
                    JTable table = null;
                    if (component instanceof JTable) {
                        table = (JTable) component;
                    } else if (component != null && component.getParent() instanceof JTable) {
                        table = (JTable) component.getParent();
                    }
                    return table;
                }

                protected void clearSelection(JTable table) {
                    if (table != null) {
                        table.clearSelection();
                    }
                }

                protected void stopCellEditing(JTable table) {

                    if (table != null) {
                        TableCellEditor cellEditor = table.getCellEditor();
                        if (cellEditor != null) {
                            if (!cellEditor.stopCellEditing()) {
                                cellEditor.cancelCellEditing();
                            }
                        }
                    }
                }
            }, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
        }

    }

}

此示例基本上适用于所有 JTable(一旦 AWTEventListener 已注册),但您可以通过更改某些事件将其配置为监视单个 table来源并将它们相互比较:P

我以前用 JTree 做过这个。焦点侦听器等不起作用。最后我使用的解决方案是使用 Toolkit.getDefaultToolkit().addAWTEventListener 并捕获键盘和鼠标事件。在事件的组件层次结构中查找 JTree,如果该项目不属于树,则调用方法删除选择。

需要的话我可以搜一下代码

编辑

接受的答案中的 AWTEventListener 比需要的要复杂一些。

        final AWTEventListener focusTracker = new AWTEventListener() {
            @Override public void eventDispatched(AWTEvent event) {
                if (event.getID() != MouseEvent.MOUSE_CLICKED && event.getID() != KeyEvent.KEY_PRESSED)
                    return;
                if (!isPartOfTable((Component) event.getSource())) {
                    if (table.isEditing()) {
                        TableCellEditor cellEditor = table.getCellEditor();
                        cellEditor.cancelCellEditing();
                    }
                    table.clearSelection();
                    table.dispatchEvent(new FocusEvent(table, FocusEvent.FOCUS_LOST));
                }
            }

            protected boolean isPartOfTable(Component component) {
                while (component != null && component != table)
                    component = component.getParent();
                return component == table;
            }

        };
        Toolkit.getDefaultToolkit().addAWTEventListener(focusTracker, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);

注意:关闭框架时需要移除监听器。我不喜欢 table 单元格在外部单击时仍然突出显示。所以我将焦点丢失事件发布到 table。