aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Making JTable uneditable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making JTable uneditable" Watch "Making JTable uneditable" New topic
Author

Making JTable uneditable

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
Anyone know how to make the cells in a JTable uneditable (by the program user) but still editable by the program itself (using setValueAt(Object val, int row, int column))?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Preferably override that method for the table model, not the table itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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 ]
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making JTable uneditable