java JTable 禁用单元格中的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5798980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:48:31  来源:igfitidea点击:

JTable disable Checkbox in Cell

javaswingjtablerenderer

提问by Yannis Assael

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

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

回答by trashgod

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 enabledstate in your table model.

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

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

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

disabled image

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 DateRendereris evoked because the TableModelreturns the type tokenDate.class, for which it has been made the default.

附录:在上面的示例中,DateRenderer被唤起,因为TableModel返回类型 tokenDate.class,它已被设为默认值。

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

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

通过如下所示的覆盖可以获得相同的外观prepareRenderer(),但该方法将针对所有单元格调用,而与类无关。因此,prepareRenderer()非常适合影响整行,如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;
    }
};