Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to make JTable not editable ?  RSS feed

 
Mai Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a program that allows the user to make a search. The result of the search is shown in a JTable.
The user can make as many searches as he wants.

Everytime he makes a search, I am repopulating the JTable again without setting the model again.

The thing is the code that sets the table to be not editable which is the following :



is part of the model's method which is accessed only one time, the first time the user does a search.

How can I set my table to be not editable ?


Thank you
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't recreate the JTable or the TableModel then is should work.

If it doesn't work then you are recreating one or both and not overriding the isCellEditable(...) method the second time.
 
Mai Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok here's the code because it's so confusing me this not editable thing:


I have another class that calls those methods when a search is done. the code for calling the methods is :


 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see in your code the first time you do a search you create a JScrollPane and a TableModel that is not editable.

For every other search you use the variable "modele" and I have no idea where that model is created. It obviously does not override the isCellEditable(...) method.

You have a rather complicated design.

A simpler design would be to create a JTable at the start of your program with the DefaultTableModel with just the column names and no data.

Then in your search method you would use:



to remove all the data from the model.

Then in your while loop you can just create a Vector for each row of data you want to add:




Whatever you do, the key point is to only keep a reference to the DefaultTableModel when you first create the model. In the logic you posted, you never return a reference to the DefaultTableModel so I don't know what "modele" is initialize to. Using my suggestion you would create the table and model at the same time, so you would only ever have one reference to each.
 
Mai Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention this part of the code:

 
Mai Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try what you suggest it and gt back to you.


Thank you
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And that is exactly the problem. You area creating a DefaultTableModel but you didn't override the isCellEditable() method.

 
Mai Naz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I did override the method isCellEditable, it's in line 89 which sets the return to false.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two pieces of code which return DefaultTableModel objects. (The piece of code you posted originally, and the one you posted later because you forgot it earlier.) One of them produces an editable table model and the other produces an uneditable table model. In this thread it has been explained how to make your table model uneditable, so you should apply that knowledge to your code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!