This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Editable cells in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Editable cells in a JTable" Watch "Editable cells in a JTable" New topic
Author

Editable cells in a JTable

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Hello!

I am working on a project for school, in which I have a JTable. The assignment requires a "Modify" button that actually works. In my JTable, the default (of course) is set for editable, however my modify button has to do something other than just look good! If I override the default setting to uneditable, is it possible to write a method that will override that setting once the modify button is pressed, or am I spinning my wheels? How, should this be approached?

Thanks in advance....

Debra Simeroth
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.
Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Rob Camick wrote:The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.


Hello,

OK, I tried that method, but am messing up somewhere... the code compiles, and runs, without the desired results... I tried adding a getFocus method, but I couldn't get that to compile... what am I doing wrong?



I wish I had words to tell you all how discouraged I am with this project! Every little button takes hours of trying, and I still have to go somewhere for help! Is it just me or is this stuff really hard at first?... I still have to add the buttons "add, delete and search"! ....Anyway, thanks for listening to me grumble and for the help (in advance).

Debra

P.S. pointers for a good direction in my future buttons are welcome....
P.P.S Why am I a Ranch Hand? I am so green I am yellow! I'm really just a greenhorn!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
Yet again you posted 300 lines of code to test out a concept. This SSCCE should be no different than the last one. All you need is a table and a button.

Is it just me or is this stuff really hard at first?...


Its hard at first until you develop the problem solving skills. Although this one is more difficult than the last one. Once I see a SSCCE I might have a simple solution for you (if I understand your requirement).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Debra Simeroth wrote:P.P.S Why am I a Ranch Hand? I am so green I am yellow! I'm really just a greenhorn!

Number of posts. After a certain number you get promoted to Ranch Hand automatically. Any other status you really do have to earn

I see in your code you still use DefaultTableModel. That one always returns true from isCellEditable. You will need a custom TableModel. A quick example:
You call setEditable in the action listener of your button. This does require you to have a field to store a reference to your table model in.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Hi Again....

I really need some sleep, but can't because I am so stressed out about this project...
Below is some test code, however I can't call the method setEditable to even test it....



I guess the good new is that I will pass the class even if I don't turn this in, but I will mess up a really pretty GPA.
Again, thanks for your patience and tutoring... it is truly appreciated.

Deb
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

First of all, line 46 is trying to access the DefaultTableModel variable declared on line 32. However, this variable is a method local variable, and cannot be access outside of the Frame constructor. Make it a private field:
As you can see, I changed both declaration and construction of dtm to use the new WantToEdit class. If you don't you can't call setEditable on it.

This does require a constructor which I left out of my example, but the comment said you should create them similar to that of DefaultTableModel. In short, this is the one you need:
Now there is still one compiler error: the call to setEditable on line 46 requires a boolean parameter. So make it setEditable(true). Now your class will compile and run.

That leaves one comment: on line 53, you give WantToEdit a DefaultTableModel instance field. Remove this; WantToEdit doesn't need it - it IS a DefaultTableModel.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
Another quick example, maybe a little trickier solution than the one you where working on, but it shows the basic concept that if you want to change the property of an object then you need a reference to that object.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Editable cells in a JTable