• 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

is it possible to let jComboBox display the object's string field in th DefaultComboBoxModel Vector

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to let jComboBox display the object's string field(without Override object class's toString() method)
of th DefaultComboBoxModel Vector.
if writing my own dataModel a good solution? because there are more than one combobox in the panel, is it need to implements all the combox datamodel? or only one model are enough?
thanks in advance.

example:


 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that I understand your question here. Could you clarify it a bit? One thing I do notice is that the combo box's text makes sense only when we uncomment the toString() method in your City class, and this is expected behavior.

for those without NetBeans, here's his code cleaned of NetBeans-generated code:
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default JComboBox, JList, JTable and JTree use toString() for formatting. You can override this by supplying renderers: ListCellRenderer for the first two, TreeCellRenderer and TableCellRenderer. You can start by overriding DefaultListCellRenderer et all:
By only changing value if it is an instance of your class you add some safety in case the JList accidentily contains something else. Of course you can tweak a lot more, but these are the basics.
 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:By default JComboBox, JList, JTable and JTree use toString() for formatting. You can override this by supplying renderers: ListCellRenderer for the first two, TreeCellRenderer and TableCellRenderer. You can start by overriding DefaultListCellRenderer et all:
By only changing value if it is an instance of your class you add some safety in case the JList accidentily contains something else. Of course you can tweak a lot more, but these are the basics.



thanks for your replies,guys.
solved.
 
reply
    Bookmark Topic Watch Topic
  • New Topic