JTable 禁用单元格中的复选框 [英] JTable disable Checkbox in Cell

查看:30
本文介绍了JTable 禁用单元格中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

Date.class,已将其设为默认值.

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

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

private final JTable table = new JTable(model) {@覆盖公共组件准备渲染器(TableCellRenderer 渲染器,整数行,整数列){组件 c = super.prepareRenderer(renderer, row, col);如果(列 == DATE_COL){日历日历 = Calendar.getInstance();calendar.setTime((Date) model.getValueAt(row, col));c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);}返回 c;}};

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!!!

解决方案

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.

@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;
    }
};

这篇关于JTable 禁用单元格中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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