Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to make JTable not editable ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3244
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3244
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will try what you suggest it and gt back to you.


Thank you
 
Rob Camick
Rancher
Posts: 3244
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, I did override the method isCellEditable, it's in line 89 which sets the return to false.
 
Sheriff
Posts: 26765
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic