• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with renderer for a list of objects to a JComboBox in a JTable

 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having difficulty getting a combo box of objects to display correctly in a JTable.
I have created a combo box with a NationalityList of Nationality objects:
// JComboBox nationalityComboBox = new JComboBox();
// DefaultComboBoxModel nationalityComboModel = new DefaultComboBoxModel();
// NationalityComboRenderer nationalityRenderer;//(NationalityComboRenderer extends JLabel implements ListCellRenderer)
try {
StaticDelegate delegate = StaticDelegate.getInstance();
NationalityList list = delegate.getAllNationalities();
this.nationalityComboModel.addElement("<no nationality selected>");
for (int i = 0; i < list.size(); i++) {
this.nationalityComboModel.addElement(list.getNationality(i));
}
nationalityComboBox.setModel(nationalityComboModel);
nationalityComboBox.setRenderer(nationalityRenderer);
} catch (BusinessDelegateException e) {
log.info("Unable to retrieve data.", e);
}
I set the cell editor as new DefaultCellEditor(nationalityComboBox).
I can display the combo box on a form correctly with the nationality descriptions, but when adding it to the JTable just get a list of object descriptions (their jar references and the memory location) as strings displayed.
I'd be grateful to anyone who can offer advice on how I can get this combo box with the list of nationalities into the JTable with the nationality descriptions displayed correctly!
 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you looked at the swingset demo that comes with the SDK?
There is a complete example on JTables and their models and renderers (with comboboxes).
You might have to wittle all the excess out to get down to a decent skeleton but its worth the effort.
Otherwise check the Swing tutorial at the sun website.
chris.
 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already checked there to get a combo box using strings which works fine, but to use objects rather than strings is proving to be the headache.
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding a toString() method to your StaticDelegate class. The renderer will use it instead of the default provided.
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted too quickly. I should have said the toString() should be added to the objects in your model, the objects returned from list.getNationality(i).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!