单击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
我所追求的行为是
目前发生的是步骤3和步骤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);
}
}
答案 0 :(得分:1)
我已经遇到过这个问题:确定哪个事件触发了选择更改。解决方案是:
if (EventQueue.getCurrentEvent() instanceof MouseEvent) {
// handler for mouse
} else if (EventQueue.getCurrentEvent() instanceof KeyEvent) {
// handler for key
}
只需在ListSelectionListener中使用它即可。我不确定这种方法是否绝对正确,但它对我有用。
感谢@mKorbel。还有另一种可能性来检查是否按鼠标或键盘进行了选择更改。详情here。
答案 1 :(得分:0)
这是我的解决方案,虽然感觉有点像黑客和有点臭,因为你必须使用来自另一个事件的信息 - getValueIsAdjusting()。它只能在mousePressed()中使用。
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 {
private static boolean isValueAdjusting = false;
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) {
System.out.println("mouseClicked");
}
@Override
public void mousePressed(MouseEvent e) {
final int row = table.rowAtPoint(e.getPoint());
if (row >= 0 && table.isRowSelected(row) && !isValueAdjusting) {
System.out.println("mousePressed on selected row " + row);
} else {
System.out.println("mousePressed on un-selected row " + 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) {
isValueAdjusting = e.getValueIsAdjusting();
System.out.println(e);
}
});
frame.add(table, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}