这个监听器在 95% 的时间里工作:
messagesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
messagesJListValueChanged(evt);
}
});
但是,它有时会在不方便的时候注册。毫无疑问,我的错误处理是根本问题。话虽这么说,是否有一个替代的监听器可以聚合各种鼠标和键盘监听器,但仅限于那些事件?
最佳答案
This listener works 95% of the time:
在所有情况下都适用于我,确保需要测试 selectedItem、Index 或 Row
是否大于 -1(无选择)
jList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int selectedRow = jList.getSelectedIndex();
if (selectedRow> -1) {
System.out.println("selection");
}
}
}
});
关于java - Swing 中的鼠标和键盘监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658340/