即使在Java中的jTable中禁用行选择后,如何保持当前选中的行突出显示 [英] How to keep the currently selected rows highlighted even after disabling the row selection in jTable in java

查看:215
本文介绍了即使在Java中的jTable中禁用行选择后,如何保持当前选中的行突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有一个JTable,其中包含5行(可以说)UI.

I have a JTable in java which contains 5 rows(lets say) UI.

我选择2行,并高亮显示这2行,然后单击一个按钮.现在在代码中,在该按钮上,我禁用了行选择,因此单击该按钮后,我将无法再选择其他行.

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.

但是问题在于这2行的选择已清除,这意味着在代码中我可以访问这2个选定的行,但是在UI中,禁用后这些行未突出显示.

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.

即使禁用行选择后,有什么方法可以保留UI中的行选择?

Is there any way that I can keep that selection of rows in the UI even after disabling the row selection??

推荐答案

创建一个自定义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.

这篇关于即使在Java中的jTable中禁用行选择后,如何保持当前选中的行突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆