[英]ListSelectionModel and listener
I've a JTable with a ListSelectionModel and ListSelectionListener. 我有一个带有ListSelectionModel和ListSelectionListener的JTable。
The selection model is set in the JTables constructor: lsm.getSelectionModel()
选择模型是在
lsm.getSelectionModel()
构造函数中设置的: lsm.getSelectionModel()
and the ListSelectionListener is set via a public method: 并通过公共方法设置ListSelectionListener:
public void setListSelectionListener(ListSelectionListener l){
lsm.addListSelectionListener(l);
}
called from the Controller class: 从Controller类调用:
view.setTableSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()) {
int viewRow = e.getFirstIndex();
System.out.println(viewRow + " is selected");
}
}
});
because the listener is created in another class I can't use the JTable's getSelectedRow();
因为侦听器是在另一个类中创建的,所以我不能使用JTable的
getSelectedRow();
method, but using the ListSelectionEvent object's getFirstIndex();
方法,但使用ListSelectionEvent对象的
getFirstIndex();
obviously doesn't get the current selection. 显然没有得到当前选择。
So I'm now using int viewRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex());
所以我现在使用
int viewRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex());
Does that seem like the correct way to get the current selection? 这似乎是获取当前选择的正确方法吗? It seems to be working, but I'm not sure if this is a bad way of doing it.
它似乎正在工作,但是我不确定这是否是不好的方法。 Thanks
谢谢
Only getMinSelectionIndex() and getMaxSelectionIndex() works, which returns the min and max of selected indices respectively. 仅getMinSelectionIndex()和getMaxSelectionIndex()有效,它们分别返回所选索引的最小值和最大值。 Lead/anchor selected index may >= 0 even when there is no selected row.
即使没有选定的行,引导/锚定的选定索引也可能> = 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.