使用FocusListener
附加到JTable
,这将告诉您焦点何时远离桌子。
有关详细信息,请参阅如何编写焦点侦听器
例如...
table.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
loseCellFocus();
}
});
当然,这仅在键盘焦点转移到能够接收键盘焦点的新组件时才有效
这会导致我的 lostCellFocus() 方法被立即调用,只要我点击它被调用的任何单元格
您可以使用JTable#setSurrendersFocusOnKeystroke
或检查焦点已转移到的组件是否是JTable
例如...
table.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (e.getOppositeComponent().getParent() != table) {
loseCellFocus();
}
}
});
可运行示例...
好吧,弄得乱七八糟了。我不仅必须在 . 中添加一个FocusListener
,JTable
而且还必须确保将一个添加到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 示例...
好吧,真的不想走这条路,因为它最终会陷入混乱的交叉条件,但是。基本上这会监控 ALLMouseEvent
和FocusEvent
s,它会执行一些向后的summersults 来测试有效条件(因为我们需要确保不仅 aJTable
是事件的一部分,而且编辑器是否是事件的一部分)并基于这些结果, 停止单元格编辑并清除选择...
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);
}
}
}
此示例基本上适用于 ALL JTable
(一旦AWTEventListener
注册),但您可以通过更改一些事件源并将它们相互比较来将其配置为监视单个表:P