How to keep the currently selected rows highlighted even after disabling the row selection in jTable in java

╄→尐↘猪︶ㄣ 提交于 2019-12-13 06:00:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!