Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Override JComboBox toString() behavior?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than solid dataobjects, I've got a generic class that is used to by all of my data... rather than explicit getters and setters there is a getPropery("field") method that returns me what I want.
Problem is on a JComboBox. I was using toString() when I was populating with solid objects. Now, toString() could mean anything.
Is there a way to overide either the comboBox or the model to tell it to display what I want? To make it configureable?
Thanks.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cy,
Pardon me if this is a silly suggestion, but have you thought about subclassing "JComboBox" (or the "model") and overriding the "toString()" method (perhaps :-)
Hope this helps.
Good Luck,
Avi.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I'd go back to the data objects. It makes more OO sense. However, it is very easy to implement a model or renderer for you generic class.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cy,
As Dana said. A custom renderer is what u r looking for. Also as he suggested consider going back to classes/objects those real fundas of OOPS
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cy Bird,
Just wondering, have u considered going back to OOPS funda yet.
The pattern that u r using is a "Singleton Pattern" and should be used when there should be a single instance of any class. Typically such classes r managers in the design. They manage resources and defines commn protocol betn different objects.
Some e.g. :- A manager managing print services, ApplicationManager keeping registry of applications running in an multi Application environment. etc.
Do u know doing this for every objects will be horrible to maintain large projects. The major prob with this pattern is that generally such classes object reference should be got thru a public static method. So to give subclasses change to override behaviour such classes should look up environment variable like System.getProperty("myclass") to initiate the instance. In ur case then all the classes should have to look up the environment.
Hope u'll reconsider ur design decision.
Sincerely,
Ashish
 
Cy Bird
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies... I ended up extending JComboBox. My new comobbox now holds a map of all my elements. The ID is the key, and the value is my my custom bean. Very similar to the hard objects, just a little lighter and more cusomizable. The only reason we're going this way (we're using DynaBeans btw) is for memory purposes. Our app has to run on really slow machines and we have a lot of gui-rich features. Just trying to eliminate some weight...
 
I can't take it! You are too smart for me! Here is the tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic