My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://mindsiview.wordpress.com/ and update your bookmarks.

Wednesday, February 21, 2007

Making a JTable Non Editable

There are times when you want to display a JTable in your Swing app, but do not want to the user to be able to edit the cells. Here's a simple way to accomplish this task via the DefaultTableModel:
  DefaultTableModel model = new DefaultTableModel(6,7){
   
 public boolean isCellEditable(int row, int column)
 {
     return false;
 }
  };
In the example above, I'm creating a DefaultTableModel model that has 6 rows and 7 columns. I'm overriding the isCellEditable() method, the method used by other methods and classes to know whether or not a JTable cell can be edited, and returning a boolean value of false in all cases. I could have also returned false for only selected rows and columns by adding conditional logic .

8 comments:

Anonymous said...

Great! It was difficult to find such an elegant solution...

Anonymous said...

Wow! Kudos to you! All other posts were about making a table renderer which is a complicated answer to a simple problem. But this takes the cake! Great job! =)

Anonymous said...

Thank you very much

Anonymous said...

Great solutions are usually simple.
Thanks for posting this example!

Anonymous said...

you are great! very simple solution. Other people recommends going through tutorials.... Hello? we have already gone through not-so informational tutorials!

Rick said...

Glad you like this solution. I'm a simple guy and I don't like complicated solutions to simple problems.

Anonymous said...

I am from India. I really liked your solution; simple, elegant and logical. Can you tell me how to make the text in a row, wrap around in the table itself rather than going out of the panel, so that a next row is automatically formed when text exceeds the width of a row.

Anonymous said...

That's perfect ;p
Thanks man!!!

You save me .รต/

Technorati search