单击JTable单元格时,该行变为“已选中”,我想要它,以便当我单击其他任何内容时,它将变为未选中状态。
我正在考虑在桌面上使用鼠标监听器执行此操作但不确定如何识别(单击不在桌面上)。有什么想法吗?
这就是我正在尝试的:
jTable.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
System.out.println("click");}});
但是当我点击第一列时它只打印点击,当我点击不是表格的东西时肯定不会打印。
当我认识到这个事件时,我会称这个方法为:
public void loseCellFocus()
{
jTable.getCellEditor().stopCellEditing();
jTable.clearSelection();
}
答案 0 :(得分:1)
使用附加到FocusListener
的{{1}},这会告诉您焦点何时远离桌子。
有关详细信息,请参阅How to Write a Focus Listener
例如......
JTable
当然,这仅在将键盘焦点转移到能够接收键盘焦点的新组件时才有效
这会导致我的lostCellFocus()方法立即被调用,只要我点击它所称的任何单元格
您可以使用JTable#setSurrendersFocusOnKeystroke
或检查焦点转移到的组件是否是 table.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
loseCellFocus();
}
});
的孩子
例如......
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);
}
}
}
好吧,并不是真的想走这条路,因为它最终会遇到一堆交叉条件,但是。基本上这会监视所有MouseEvent
和FocusEvent
s,它会执行一些向后的结果来测试有效条件(因为我们需要确保不仅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
注册),但您可以将其配置为监视单个表,方法是更改某些事件源并将它们与每个事件源进行比较其他:P
答案 1 :(得分:1)
我之后用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);
注意:您需要在关闭框架时删除侦听器。我不喜欢在外面点击时仍然突出显示表格单元格。所以我在桌子上发布一个焦点丢失的事件。