问题
I have a JTable
in java which contains 5 rows(lets say) UI.
I select 2 rows and those 2 rows are highlighted and then I click on a button. 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.
Is there any way that I can keep that selection of rows in the UI even after disabling the row selection??
回答1:
Create a custom ListSelectionModel
that allows you to toggle the selection state of the model.
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.
回答2:
without a code example it is difficult to help. But I would recommend to set css classes for the selected rows. With jQuery you can just do it like:
$(selector).addClass('yourCSSClass')
You can also delete the css class with .removeClass.
Or you can use .toggleClass which adds or removes the css class.
来源:https://stackoverflow.com/questions/31802280/how-to-keep-the-currently-selected-rows-highlighted-even-after-disabling-the-row