简体   繁体   English

JTable set disabled复选框查找不可编辑的单元格

[英]JTable set disabled checkbox look for uneditable cell

I have JTable with a boolean values column. 我有一个布尔值列的JTable Depending on the state stored in model I make some or all of them uneditable (model's isCellEditable() returns false). 根据模型中存储的状态,我可以使其中的部分或全部不可编辑(模型的isCellEditable()返回false)。 However this does not make the JTable boolean renderer to render the checkboxes as disabled for uneditable cell. 但是,这不会使JTable布尔渲染器将复选框呈现为对不可编辑单元格禁用。

Is there a way how to achive this other than writing custom boolean renderer? 除了编写自定义布尔渲染器之外,有没有办法解决这个问题?

If I need to write my own renderer what class should I extend other than JCheckbox ? 如果我需要编写自己的渲染器,除了JCheckbox之外,我应该扩展哪个类? I just simply need to disable the checkbox before rendering and do not want to implement all the rendering code and handle selected look and stuff. 我只是需要在渲染之前禁用复选框,并且不希望实现所有渲染代码并处理选定的外观和内容。

However this does not make the JTable boolean renderer to render the checkboxes as disabled for uneditable cell. 但是,这不会使JTable布尔渲染器将复选框呈现为对不可编辑单元格禁用。

This is correct, because it's the default renderer's behavior: JCheckBox is uneditable but not disabled . 这是正确的,因为它是默认渲染器的行为: JCheckBox是不可编辑的但未禁用

Is there a way how to achive this other than writing custom boolean renderer? 除了编写自定义布尔渲染器之外,有没有办法解决这个问题?

No, as far as I know. 不,据我所知。

If I need to write my own renderer what class should I extend other than JCheckbox? 如果我需要编写自己的渲染器,除了JCheckbox之外,我应该扩展哪个类?

It's not mandatory to extend any class to implement TableCellRenderer interface. 扩展任何类以实现TableCellRenderer接口并不是必需的。 You can perfectly have a JCheckBox as renderer's class member. 您可以完美地将JCheckBox作为渲染器的类成员。 Actually, composition is preferred over inheritance. 实际上,组合比继承更受欢迎。

I just simply need to disable the checkbox before rendering and do not want to implement all the rendering code and handle selected look and stuff. 我只是需要在渲染之前禁用复选框,并且不希望实现所有渲染代码并处理选定的外观和内容。

It's not that difficult and you can control what is happening. 这并不困难,你可以控制正在发生的事情。 Consider the example below: 考虑以下示例:

class CheckBoxCellRenderer implements TableCellRenderer {

    private final JCheckBox renderer;

    public CheckBoxCellRenderer() {
        renderer = new JCheckBox();
        renderer.setHorizontalAlignment(SwingConstants.CENTER);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Color bg = isSelected ? table.getSelectionBackground() : table.getBackground();
        renderer.setBackground(bg);
        renderer.setEnabled(table.isCellEditable(row, column));
        renderer.setSelected(value != null && (Boolean)value);
        return renderer;
    }
}

See this Q&A for a related problem: JXTable: use a TableCellEditor and TableCellRenderer for a specific cell instead of the whole column 有关相关问题,请参阅此问答: JXTable:对特定单元格使用TableCellEditor和TableCellRenderer而不是整列

Without a working example it is hard to say exactly what is wrong, but it sounds like you may have forgotten to fire a table modification event to notify the JTable it needs to repaint itself. 如果没有一个工作示例,很难确切地说出错误是什么,但听起来您可能忘记触发表修改事件来通知它需要重绘自己的JTable。 You need something like this in your model when you make your change: 当您进行更改时,您需要在模型中使用以下内容:

fireTableChanged(new TableModelEvent(sourceModel, firstRow, lastRow, tableCol));

There are different table change events you can fire and different parameters you can pass to the TableModelEvent constructor. 您可以触发不同的表更改事件以及可以传递给TableModelEvent构造函数的不同参数。 You can find more information in the Javadocs here: http://docs.oracle.com/javase/8/docs/api and the Java Tutorials here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#fire . 您可以在Javadocs中找到更多信息: http//docs.oracle.com/javase/8/docs/api和Java教程: http//docs.oracle.com/javase/tutorial/uiswing/components /table.html#fire You will need to have a read of these to work out which ones are right for your particular circumstances. 您需要阅读这些内容,以确定哪些适合您的特定情况。

I doubt a custom boolean renderer would be required. 我怀疑是否需要自定义布尔渲染器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM