选中 JTable 中的复选框时发出通知

发布于 2024-09-18 19:26:46 字数 237 浏览 2 评论 0原文

我已经搜索了很长一段时间,但没有在任何地方找到明确的例子。我是一名使用 NetBeans 的 Java 新手。我在 JTable 的第一列中有一个布尔值(称为“Enabled”),并且我有一些插件代码,我需要调用它们来查看它是否具有启用所需的设置,如果没有,则显示消息框并阻止选中“已启用”。

我真正需要的是在选中复选框时调用一个函数,然后我可以从那里获取它。有人有如何执行此操作的示例吗?

感谢您的帮助!

哈利

I've searched for this for quite a while and haven't found a clear example anywhere. I'm a Java newbee using NetBeans. I have a boolean value in the first column of a JTable (called "Enabled") and I have some plugin code that I need to call to see if it has the settings it needs in order to be enabled, and if not, display a message box and prevent Enabled from being checked.

All I really need is for a function to be called when the checkbox is checked and I can take it from there. Does anyone have an example of how to do this?

Thanks for your help!

Harry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-09-25 19:26:59

我真正需要的是一个函数
选中复选框时调用

当选中复选框时,模型中的值将被更改,这可能不是您想要的。我认为您首先要阻止检查该复选框。

防止单元格可编辑的方法是重写 JTable 的 isCellEditable(...) 方法。通过重写此方法,您可以动态确定单元格是否可编辑。

JTable table = new JTable( ... )
{
    public boolean isCellEditable(int row, int column)
    {
        int modelColumn = convertColumnIndexToModel( column );

        if (modelColumn == yourBooleanColumn)
            return isTheBooleanForThisRowEditable(row);
        else
            return super.isCellEditable(row, column);
    }
};

一种更奇特的方法是创建一个自定义渲染器,以便即使在用户尝试单击单元格之前,该复选框也看起来“已禁用”。请参阅trashgod 在渲染器上提供的链接。

All I really need is for a function to
be called when the checkbox is checked

When the checkbox is checked then the value will be changed in the model, which is probably not what your want. I would think you want to prevent the checking of the checkbox in the first place.

The way to prevent a cell from being editable is to override the isCellEditable(...) method of JTable. By overriding this method you can dynamically determine if the cell should be editable or not.

JTable table = new JTable( ... )
{
    public boolean isCellEditable(int row, int column)
    {
        int modelColumn = convertColumnIndexToModel( column );

        if (modelColumn == yourBooleanColumn)
            return isTheBooleanForThisRowEditable(row);
        else
            return super.isCellEditable(row, column);
    }
};

And a fancier approach would be to create a custom renderer so that the check box looks "disabled" even before the user attempts to click on the cell. See the link provided by trashgod on renderers.

衣神在巴黎 2024-09-25 19:26:55

您可能需要一个 TableModelListener,如 监听中所述用于数据更改。或者,您也可以使用自定义编辑器,如概念:编辑器和渲染器以及以下部分。

You probably want a TableModelListener, as discussed in Listening for Data Changes. Alternatively, you can use a custom editor, as discussed in Concepts: Editors and Renderers and the following section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文