gpt4 book ai didi

java - 单击 Swing JTable 中的行时出现意外的事件顺序

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:37 24 4
gpt4 key购买 nike

单击 JTable 中的行时的默认行为似乎是...

1 event=javax.swing.event.ListSelectionEvent[ source=javax.swing.DefaultListSelectionModel 673443626 ~{0} firstIndex= 0 lastIndex= 0 isAdjusting= true ]
2 mousePressed
3 event=javax.swing.event.ListSelectionEvent[ source=javax.swing.DefaultListSelectionModel 673443626 ={0} firstIndex= 0 lastIndex= 0 isAdjusting= false ]
4 mouseReleased
5 mouseClicked

即使鼠标单击正在启动操作,JTable 事件也会在 mousePressed 之前潜入。为什么不是……

1 mousePressed
2 event=javax.swing.event.ListSelectionEvent[ source=javax.swing.DefaultListSelectionModel 673443626 ~{0} firstIndex= 0 lastIndex= 0 isAdjusting= true ]
3 event=javax.swing.event.ListSelectionEvent[ source=javax.swing.DefaultListSelectionModel 673443626 ={0} firstIndex= 0 lastIndex= 0 isAdjusting= false ]
4 mouseReleased
5 mouseClicked

我追求的行为是

  1. 点击一行
  2. 在 mousePressed 中,检查是否选择了行?
  3. 否 - 只需选择行。
  4. 是 - 执行一些操作。

目前发生的情况是,当单击未选定的行时,步骤 3 和 4 会同时发生。没有可用于检测该行是否被选中的鼠标事件 - 它将始终被选中。

或者换句话说...当您用鼠标单击 JTable 中的一行时,如何在单击事件期间测试该行是否在单击事件之前未被选中?

这是一个 SSCCE...

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JTableRowSelectTest {

public static void main(String args[]) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Object rowData[][] = {{"Row1-Column1"}, {"Row2-Column1"}};
Object columnNames[] = {"Column One"};
final JTable table = new JTable(rowData, columnNames);

table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
final int row = table.rowAtPoint(e.getPoint());
if (row >= 0 && table.isRowSelected(row)) {
System.out.println("mouseClicked on selected row");
} else {
// never reached.
System.out.println("mouseClicked on un-selected row");
}
}

@Override
public void mousePressed(MouseEvent e) {
final int row = table.rowAtPoint(e.getPoint());
if (row >= 0 && table.isRowSelected(row)) {
System.out.println("mousePressed on selected row");
} else {
// never reached.
System.out.println("mousePressed on un-selected row");
}
}

@Override
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}
});

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
System.out.println(e);
}
});

frame.add(table, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);

}
}

最佳答案

我已经遇到了这个问题:确定哪个事件触发了选择更改。解决办法是:

if (EventQueue.getCurrentEvent() instanceof MouseEvent) {
// handler for mouse
} else if (EventQueue.getCurrentEvent() instanceof KeyEvent) {
// handler for key
}

只需在 ListSelectionListener 中使用它即可。我不确定这种方法是否绝对正确,但它对我有用。

感谢@mKorbel。还有另一种可能性来检查选择更改是否是通过鼠标或键盘进行的。详情here .

关于java - 单击 Swing JTable 中的行时出现意外的事件顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25907061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com