tags:

views:

213

answers:

2

I have added a JCheckBox in a cell of a JTable. But when the frame containing the JTable loads i can not see the JCheckBox in the JTable. Instead of the component it shows true/false values of the JCheckBox when i click on that cell.

checkbox_column=table.getColumnModel().getColumn(4); checkbox_column.setCellEditor(new DefaultCellEditor(checkbox));

Also how to disable the column re positioning when you drag it in JTable?

+1  A: 

You set the cellEditor which handles in-place edits of the cell. The general painting of the cell is handled by the cell renderer which is set via setCellRenderer()

To disable column reordering, you want

table.getTableHeader().setReorderingAllowed(false);
Devon_C_Miller
+1  A: 

Remember, the data model (more specifically, the TableModel) is separate from the view.

You should set the value of the cell to a Boolean instead of setting the value to a JCheckBox. Then make sure your TableModel's getColumnClass() method returns Boolean for column 4. There are different ways of doing this, but creating a custom TableModel and implemeting the getColumnClass() method is one way.

The DefaultCellRenderer/DefaultCellEditor will show a checkbox for Boolean values. You can also set custom renderers/editors, as Devon pointed out, but you'll still want to store Boolean values instead of JCheckBoxes in that column.

rob