Hello,
I program in Java 1.6. I wonder if it is possible to specify the cells editable and uneditable a JTable. By default, all cells are editable. Is it possible to say that some cells may not be edited? Thank you
Hello,
I program in Java 1.6. I wonder if it is possible to specify the cells editable and uneditable a JTable. By default, all cells are editable. Is it possible to say that some cells may not be edited? Thank you
It will create a custom TableModel, where you override the function isCellEdiatble ();
If you want an entire column is not editable you test only on the index of the column:Code:MyTableModel public class extends ( Override public DefaultTableModel isCellEditable boolean (int rowIndex, int columnIndex) (/ / here the cell (1, 2) is non-editable if (rowIndex == 1 & & columnIndex == 2) return false; / / the rest is editable return true;))
Code:if (columnIndex == 5) return false; / / in column 5 is not editable.
Even easier if you do not want to create an additional class for various reasons:
Code:JTable tb = new JTable (ls, cls) ( public boolean isCellEditable (int r, int c) ( return false; ) )
Have a rendered with a self-written SpinBox it. This renderer is now in every cell of my spreadsheet with this SpinBox (you can set values from 0-99) appears. Now I want but also the Value (ie the adjusted value) of the query spinBox and as I have still problems. Actually, I better each cell to the value of SpinBox ask?
I somehow understand the problem completely. If your <br> self-made SpinBox (BTW it from JDK1.4 as a swing-<br> Component exists), as cell renderers and used as CellEditor <br> is, it needs to implement even the CellEditor interface, <br> eg from javax.swing.AbstractCellEditor (JDK1.3 be derived). <br>
Somewhere in this derived class, that is in your Custom <br>
Editor, it would have something like
Code:GetCellEditorValue Object (); and setCellEditorValue (Object o) </ Code> type.
The last method is of
Code:<code> stopCellEditing () <call / code>, <br> and queries the value of your spinBox, eg <br> <code> public boolean stopCellEditing () ( SpinBox spinBox renderer.getSpinBox = (); / / get editing component setCellEditorValue (new Integer (spinBox.getValue ())); super.stopCellEditing return (); / / fire change event to listeners ) </ Code> Thus, the value of the set for the edited SpinBox <br> Cell, interrogated and sent to JTable. <p>
Bookmarks