[英]How to keep the currently selected rows highlighted even after disabling the row selection in jTable in java
I have a JTable
in java which contains 5 rows(lets say) UI. 我在Java中有一个
JTable
,其中包含5行(可以说)UI。
I select 2 rows and those 2 rows are highlighted and then I click on a button. 我选择2行,并高亮显示这2行,然后单击一个按钮。 Now in the code, on that button I am disabling the row selection so that I will not be able to select any more rows once I have clicked that button.
现在在代码中,在该按钮上,我禁用了行选择,因此单击该按钮后,将无法再选择任何行。
But the problem is the selection of those 2 rows is getting cleared means that in the code I can access those 2 selected rows but in the UI those rows are not highlighted after disabling. 但是问题在于,这2行的选择已清除,这意味着在代码中我可以访问这2条选定的行,但是在UI中,禁用后这些行未突出显示。
Is there any way that I can keep that selection of rows in the UI even after disabling the row selection?? 有什么方法即使禁用行选择后也可以保留UI中的行选择?
Create a custom ListSelectionModel
that allows you to toggle the selection state of the model. 创建一个自定义
ListSelectionModel
,它允许您切换模型的选择状态。
public class ToggleListSelectionModel extends ListSelectionModel
{
private boolean selectionEnabled = true;
public ToggleListSelectionModel()
{
super();
}
public boolean isSelectionEnabled()
{
return selectionEnabled;
}
public void setSelectionEnabled(boolean selectionEnabled)
{
this.selectionEnabled = selectionEnabled;
}
@Override
public void addSelectionInterval(int index0, int index1)
{
if (selectionEnabled)
super.addSelectionInterval(index0, index1);
}
// Override other add/remove methods
}
Then in the ActionListener of your button you can disable the selection state. 然后,可以在按钮的ActionListener中禁用选择状态。
without a code example it is difficult to help. 没有代码示例,很难提供帮助。 But I would recommend to set css classes for the selected rows.
但是我建议为选定的行设置css类。 With jQuery you can just do it like:
使用jQuery,您可以像这样:
$(selector).addClass('yourCSSClass')
You can also delete the css class with .removeClass. 您也可以使用.removeClass删除css类。
Or you can use .toggleClass which adds or removes the css class. 或者,您可以使用.toggleClass来添加或删除css类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.