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?
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.
Joined: Jun 11, 2009
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.
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).
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!
Joined: Jun 13, 2009
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).
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.
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.
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.