Jtable Disable Checkbox in Cell

JTable disable Checkbox in Cell

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

Set JTable Cell Disable or Enable based on condition?

Based on this example, the code below conditions the isCellEditable() implementation based on the model's state for that row. Once checked, a row cannot be unchecked. You can alter the appearance of such rows as desired using a suitable renderer, as shown here.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
* @see https://stackoverflow.com/a/31082475/230513
* @see https://stackoverflow.com/questions/7920068
* @see https://stackoverflow.com/questions/4526779
*/
public class CheckOnce extends JPanel {

private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.FALSE}, {"Two", Boolean.FALSE},
{"Three", Boolean.FALSE}, {"Four", Boolean.FALSE},
{"Five", Boolean.FALSE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.FALSE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.FALSE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);

public CheckOnce() {
super(new BorderLayout());
this.add(new JScrollPane(table));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setPreferredScrollableViewportSize(
new Dimension(250, 10 * table.getRowHeight()));
}

private class DataModel extends DefaultTableModel {

public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}

@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
}

@Override
public Class<?> getColumnClass(int col) {
if (col == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(col);
}

@Override
public boolean isCellEditable(int row, int col) {
Object o = getValueAt(row, col);
boolean b = o instanceof Boolean && (Boolean) o;
return col == CHECK_COL && !b;
}
}

private static void createAndShowUI() {
JFrame frame = new JFrame("CheckOne");
frame.add(new CheckOnce());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowUI();
}
});
}
}

JTable enable and disable checkbox by checking one in other column

I haven't looked at your code, but in general with Swing tables, lists and trees, the renderer instance is reused to draw many rows. You need to tell the JTable that its contents has changed, so that it may redraw the relevant rows. Use the model to notify its TableModelListeners that some rows have changed.

Checkbox in only one JTable Cell

You have stated in a comment:

I tried it with a column of JCheckboxes it works but I just want it in
one cell

Note this requirement is a little tricky. I think you can override getCellRenderer(int row, int column) and getCellEditor(int row, int column) methods asking for the cell value's class.

This way the cell renderer/editor will be a JCheckbox even when the table's rows are sorted or table's columns are rearranged.

Something like this:

    JTable table = new JTable(model) {
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
if(getValueAt(row, column) instanceof Boolean) {
return super.getDefaultRenderer(Boolean.class);
} else {
return super.getCellRenderer(row, column);
}
}

@Override
public TableCellEditor getCellEditor(int row, int column) {
if(getValueAt(row, column) instanceof Boolean) {
return super.getDefaultEditor(Boolean.class);
} else {
return super.getCellEditor(row, column);
}
}
};

Example

Here a complete example to play with.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class Demo {

private void createAndShowGUI() {

DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0);
model.addRow(new Object[]{"Property # 1", "Value # 1"});
model.addRow(new Object[]{"Property # 2", Boolean.TRUE});
model.addRow(new Object[]{"Property # 3", "Value # 3"});

JTable table = new JTable(model) {
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
if(getValueAt(row, column) instanceof Boolean) {
return super.getDefaultRenderer(Boolean.class);
} else {
return super.getCellRenderer(row, column);
}
}

@Override
public TableCellEditor getCellEditor(int row, int column) {
if(getValueAt(row, column) instanceof Boolean) {
return super.getDefaultEditor(Boolean.class);
} else {
return super.getCellEditor(row, column);
}
}
};
table.setAutoCreateRowSorter(true);

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

Screenshots

Sample Image

How can I make a checkbox on JTable not editable after a checkbox is clicked?

but both column 2 and 3 are editable.

Well, that is what your isCellEditable(...) method states.

If you don't want column 3 to be editable then you need to modify your code. Maybe something like:

case 3:
Boolean column2 = (Boolean)getValueAt(row, 2)
return ! column2.booleanValue();


Related Topics



Leave a reply



Submit