且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使在Java中的jTable中禁用行选择后,如何保持当前选中的行突出显示

更新时间:2023-12-04 17:58:40

创建一个自定义ListSelectionModel,使您可以切换模型的选择状态.

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
}

然后在按钮的ActionListener中,可以禁用选择状态.

Then in the ActionListener of your button you can disable the selection state.