aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes is it possible to let jComboBox display the object's string field in th DefaultComboBoxModel Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "is it possible to let jComboBox display the object Watch "is it possible to let jComboBox display the object New topic
Author

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

Kevin Pang
Ranch Hand

Joined: Mar 01, 2005
Posts: 38
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Pang
Ranch Hand

Joined: Mar 01, 2005
Posts: 38
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it possible to let jComboBox display the object's string field in th DefaultComboBoxModel Vector