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 .

Advertisements

About Rick
Many years of technology experience working in both the public and private sector. I hold undergraduate degrees in Information Systems and Management as well as a Master in Business Administration. Currently I'm Vice President of Information Technology for Thomas Nelson Publishers, Inc.

9 Responses to Making a JTable Non Editable

  1. Anonymous says:

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

  2. Anonymous says:

    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! =)

  3. Anonymous says:

    Thank you very much

  4. Bert says:

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

  5. Anonymous says:

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

  6. Anonymous says:

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

  7. Rick says:

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

  8. Ankit Gupta says:

    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.

  9. Anonymous says:

    That’s perfect ;p
    Thanks man!!!

    You save me .õ/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: