GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTable cell Editor/Renderer Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable cell Editor/Renderer Question" Watch "JTable cell Editor/Renderer Question" New topic
Author

JTable cell Editor/Renderer Question

Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi All,
I am trying to put two components(a textField & a combobox) in one cell of a JTable.
Using the renderer I can display both the components in the cell.
Using the editor I can bring both the components in the editing state but now to store the values,also to pass the edited values to the renderer what should I return from CellEditor's getCellEditorValue() method.If I just return the value of one of these components from this method then everything works fine.
Any example of rendering & editing more than one component in a cell would help me a lot.
Any suggestions are welcome.
Thanks
Pomchi
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
My suggestion is to write a user-defined component that contains both your textfield and your combo box. One suggestion would be to wrap them in a JPanel and write the code to handle their behavior. This new component would then need a way to return a single value generated from BOTH the textfield and the combo box that means something. You must already know what that single value is considering that you want to store it in a cell in a table model.
Once you have your new component, simply create it in your cell editor and return the single value from your getCellEditorValue() method.
Hope this helps!


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Thanks for your reply Jeff,
"This new component would then need a way to return a single value generated from BOTH the textfield and the combo box that means something."
This is what my question is??How to return a single value from BOTH the textfield & combobox??
I tried a lot of things like storing both the values in a Vector and retuning that vector but it won't work.Can you give me any example.
Thanks again
Pomchi
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
Well, I think there are probably several ways to store these values. One would be to create an object that stores these values for you. For example:

Then have getCellEditorValue() return an instance of MyStructure that is populated with your textfield's value and your combo box's value.
Note that when you create your table model, you will need to put MyStructure objects in the column for which you want to edit. You might want to use default values (like empty strings) to initialize each MyStructure object.
In your getTableCellEditorComponent() method of your cell editor, you will then need to cast the 'value' parameter as a MyStructure object and populate your editing component with its data. Then return your editing component.
The editing component might look like:

The cell editor itself might look like:

You should be able to use the Editor class for rendering in much the same way.
NOTE: I have not compiled or run these classes, so just take the ideas from them if you like.
Anyway, I hope THIS helps.
[ January 23, 2002: Message edited by: Jeff Wisard ]
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Jeff,
Thanks for your reply!
I have solved the problem with an easy way.....though it is not the perfect way but it works fine.I am storing both the values in a String with a space between them, and I am returning this value from my getCellEditorValue() function.In the renderer class using the StringTokenizer I am displaying both the values as JLabels.
But I think apart from the solution you have suggested, we could also store values from both the components in a Vector and can return the vector from getCellEditorValue().As Vector is also an Object it should work fine....but somehow it is not working.When I try to store the values in this Vector my editor doesn't stop editing.....I mean in my table when I click on that cell then the editor component comes up but it does not loose focus and the renderer component doesn't come back.
Do you think this approach of using a Vector is wrong??
Thanks
Pomchi
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
No, I think it should work. The problem is something with the your editor component. For some reason, it won't as you said, release focus to let the table know that it is finished editing. One thing you might try is to call the stopCellEditing() method from within the editor when you have finished entering your data. That should notify the table that your editor is finished and call the getCellEditorValue() method of your editor.
Other than that, I don't think I can add anything without seeing your code.
 
GeeCON Prague 2014
 
subject: JTable cell Editor/Renderer Question