Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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:


 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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:
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic