• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable cell Editor/Renderer Question

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Pomchi Bedi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic