JTable 禁用单元格中的复选框

发布于 2024-11-03 16:13:59 字数 98 浏览 0 评论 0原文

您好,我有一个 JTable,我想将所有禁用的复选框单元格灰显,我尝试使用自定义渲染器检查 isEnabled(),然后更改背景颜色,但仍然无法正常工作。 有什么建议吗? 谢谢!!!

Hello I have a JTable And i want to grey out all the disabled checkbox cells i tried with a custom renderer checking isEnabled() and then changing the background color but still not workin.
Any suggestions?
thanks!!!

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

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

发布评论

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

评论(1

秋叶绚丽 2024-11-10 16:14:00

正如概念:编辑器和渲染器,“单个单元格渲染器通常用于绘制包含相同类型数据的所有单元格。”您需要在 表模型

附录:作为一个具体示例,此示例中的数据模型是一个简单的Date 实例数组。如下所示重写 getTableCellRendererComponent() 会导致禁用奇数天。在这种情况下,奇数是 Date 值本身固有的属性,但可以查询模型以获取任何相关属性。

disabled image

@Override
public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime((Date) value);
    Component c = super.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, col);
    c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
    return c;
}

附录:在上面的示例中,DateRenderer 被调用,因为 TableModel 返回类型标记 日期.class,它已被设为默认值。

table.setDefaultRenderer(Date.class, new DateRenderer());

可以通过重写 prepareRenderer() 来获得相同的外观,如下所示,但该方法会针对所有单元格调用,而与类无关。因此,prepareRenderer() 非常适合影响整行,如 表格行渲染

private final JTable table = new JTable(model) {

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component c = super.prepareRenderer(renderer, row, col);
        if (col == DATE_COL) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) model.getValueAt(row, col));
            c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
        }
        return c;
    }
};

As noted in Concepts: Editors and Renderers, "a single cell renderer is generally used to draw all of the cells that contain the same type of data." You'll need to maintain the enabled state in your table model.

Addendum: As a concrete example, the data model in this example is a simple array of Date instances. Overriding getTableCellRendererComponent() as shown below causes odd days to be disabled. In this case, being odd is a property inherent to the Date value itself, but the model could be queried for any related property at all.

disabled image

@Override
public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime((Date) value);
    Component c = super.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, col);
    c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
    return c;
}

Addendum: In the example above, the DateRenderer is evoked because the TableModel returns the type token Date.class, for which it has been made the default.

table.setDefaultRenderer(Date.class, new DateRenderer());

An identical appearance can be obtained by overriding prepareRenderer() as shown below, but the method is invoked for all cells, irrespective of class. As a result, prepareRenderer() is ideal for affecting entire rows, as shown in Table Row Rendering.

private final JTable table = new JTable(model) {

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component c = super.prepareRenderer(renderer, row, col);
        if (col == DATE_COL) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) model.getValueAt(row, col));
            c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
        }
        return c;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文