This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Problem JComboBox  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have following Problem, I add some Objects to a ComboBox. In the GUI i see now the objectsname and the ID. Is it possible to display instead a Attribute value of this object per Example the attribute name. You cant really identify the object over the ID.

Now its "Class@73dds676d7" and i want to show a name like "Apple juice"

Do you have any Suggestions?


Thanks

David
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "id" actually is the hashcode. What you are seeing is the return value of the toString method of the objects, which they probably inherited from java.lang.Object.

So one solution would be to override the toString method. Problem is that you might want to have a different representation for the same objects in other places.

A more flexible approach is to set a custom ListCellRenderer on the combobox.

Moving to our Swing forum...
 
David Hittel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i do that?

i have no idea how i could write this code. I would write in my gui class where the combo box is following method

public String toString(){
....
return name;
}

But what do i write in between and when do i call the method?

Thanks

David
 
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your Drink class you'll add something like this:

now you'll see your combo populated with your list of drinks
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!