简体   繁体   中英

JTable set disabled checkbox look for uneditable cell

I have JTable with a boolean values column. Depending on the state stored in model I make some or all of them uneditable (model's isCellEditable() returns false). However this does not make the JTable boolean renderer to render the checkboxes as disabled for uneditable cell.

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 ? 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.

This is correct, because it's the default renderer's behavior: JCheckBox is uneditable but not disabled .

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?

It's not mandatory to extend any class to implement TableCellRenderer interface. You can perfectly have a JCheckBox as renderer's class member. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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