This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Thank you for the advice! I figured out what to do. I made a class MyTableModel which extends DefaultTableModel and I did an override of the isCellEditable method, making it return false every time. When I started this whole business of trying to make the cells uneditable, I thought there should be some kind of method called setUneditable(int row, int column) but I guess there isn't such a method. I didn't think it would be as complicated as it was. But I did it and learned something from it! The harder something is, the more you learn from it, I guess.
While you're at it, you can also override getColumnClass to return something other than Object.class.
If for a column that method returns Object.class, it uses the toString method to display the value, and a text field to edit. By returning other classes you can change that behaviour somewhat.
Number.class, Integer.class and the classes of all other subclasses of Number will result in a right aligned column, and input validation.
Boolean.class will use a check box for both displaying and editing.
Date.class will use the default date formatting, as per DateFormat.getInstance() for both displaying and editing.
Image.class and Icon.class will have the image or icon displayed.
These can be changed, or for other classes one can be added by using the setDefaultEditor and setDefaultRenderer methods. Please note that only non-interface class objects can be used, unless the table model's getColumnClass returns an interface class. The reason is how the default renderer and editor are found: 1 get the column's class 2 check for any editor / renderer set for that class explicitly 3 if none is found goto step 2 with the super class [ March 17, 2008: Message edited by: Rob Prime ]
Joined: Oct 12, 2005
Did you read my mind?
Actually, I was wondering how to display info in a table by showing a checkbox in the table. I tried just putting a checkbox into the table directly, but the table just displayed a String, not the checkbox. (I assume that String was the result of the .toString() method of JCheckBox.) I looked around at the API a little, but couldn't figure it out, so what I eventually did was put all the checkboxes onto a panel which I layed out with a GridLayout. That put the checkboxes in rows and columns, which is what I wanted.
By the way, this table that I was trying to make with checkboxes is a different table from the one I was trying to make uneditable. The uneditable table shows Strings, and the table I was trying to make with checkboxes would have been editable.
Originally posted by Kevin Tysen: Did you read my mind?
I'm quite good at that
(I assume that String was the result of the .toString() method of JCheckBox.)
That's exactly what JTables do by default for all classes except a very select few: - Boolean (check box) - Number and subclasses (right aligned but they do use toString()) - Double and Float (right aligned, uses NumberFormat.getInstance()) - Date (uses DateFormat.getDateInstance() to ignore the time part) - Icon and ImageIcon (the image itself)
Unless you set a specific renderer either for the complete class (JTable.setDefaultRenderer) or for a specific column (using the TableColumn) those are the only options.