[英]How do I set specific columns to be selectable in a JTable?
如何确保仅JTable中的某些列是可选的(意味着它们会路由到我的ListSelectionListener)?
我添加了我的侦听器,如下所示:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {...});
最简单的解决方案可能是创建自己的选择模型:
table.getColumnModel().setSelectionModel(new DefaultListSelectionModel() {
private boolean isSelectable(int index0, int index1) {
// TODO: Decide if this column index is selectable
return true;
}
@Override
public void setSelectionInterval(int index0, int index1) {
if(isSelectable(index0, index1)) {
super.setSelectionInterval(index0, index1);
}
}
@Override
public void addSelectionInterval(int index0, int index1) {
if(isSelectable(index0, index1)) {
super.addSelectionInterval(index0, index1);
}
}
});
还要注意,如果要侦听列选择,则要将侦听器添加到列模型的选择模型(而不是表的选择模型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.