my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Understanding cell editor in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Understanding cell editor in JTable" Watch "Understanding cell editor in JTable" New topic
Author

Understanding cell editor in JTable

T Dahl
Ranch Hand

Joined: Oct 07, 2010
Posts: 35
I am trying to learn about cell editors in JTable (swing). I am now studying this example: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableFTFEditDemoProject/src/components/IntegerEditor.java

There are a few things I don't understand. I'd be happy if you could help me better understand this example.

The first line of getCellEditorValue() is:

This is declaring the local variable ftf which happens to have the same name and type as a property of the class. Not only is the name and type the same but I believe it is actually referencing the same object. If so this line seems redundant. I tried a simplified example in the debugger and i still think it is referencing the same object. Am I right? Why is it done that way?

Almost at the end of userSaysRevert() I find the following line:

This looks like i noop but I assume it's done for some side effect. What happens here?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Not sure about the local variable and instance field part, but setValue(getValue()) certainly isn't a no-op. Read the documentation for getValue() and you'll immediately spot why.
JFormattedTextField#getValue() wrote:... this may not return the current value.


luck, db
There are no new questions, but there may be new answers.
T Dahl
Ranch Hand

Joined: Oct 07, 2010
Posts: 35
Thanks!
Darryl Burke wrote:Not sure about the local variable and instance field part, but setValue(getValue()) certainly isn't a no-op. Read the documentation for getValue() and you'll immediately spot why.
JFormattedTextField#getValue() wrote:... this may not return the current value.

I did read that a few times but you made me read it a few more. I think this is meant to say "..not the currently edited value". I.e. the value that was displayed after the last commit but before any further editing. I also thought that setValue would set the exact same value.

Now, I looked up the source. I am always afraid of being intimidated when I do that . Apparently the setValue does have kind of side effects:
- It sets a new Formatter
- It resets a flag which tells if the current value has been edited.
- It fires a property change. I suppose that is done to update the displayed value.
In the sample code I suppose at least the two latter side effects are desired.

I think the purpose of the ftf.setValue(ftf.getValue()) is to cancel the editing and reset the value to whatever it was before editing started. However cancelCellEditing() seems to do the job as well in my tests.

Feel free to correct my assumptions!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding cell editor in JTable