0

This code below is meant to populate the combo box with available times according to the selected date.

However for some reason the combo box is storing the memory address of the data example:

Restaurant.Time@1a28362
Restaurant.Time@5fcf29
...

I know its getting the right times. But how do I actual print out the actual item?

TimeList times = dbConnector.selectTimes(lblDay.getText());//lblDay stores the date from the jCalendar button
cmbNewResTimes.removeAllItems();
for (int pos1 = 0; pos1 < times.size(); pos1++) {
    cmbNewResTimes.addItem(times.getTimeAt(pos1).toString());
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user667430
  • 1,487
  • 8
  • 38
  • 73
  • 1
    `with available times according to the selected date` what, when, where and how, maybe answer could be easier, for better help sooner edit your question with a [SSCCE](http://sscce.org/) – mKorbel Apr 23 '12 at 12:46

2 Answers2

7

Add Object instance

Firstly, change it to:

// add the Object, rather than the String representation.
cmbNewResTimes.addItem(times.getTimeAt(pos1));  

Set a renderer

Then set a renderer, see:

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +1 for referring to the renderer instead of overriding a toString implementation for a UI purpose – Robin Apr 23 '12 at 12:42
  • @Robin I prefer to implement rather than override generally, but (glances up) it can have some pretty results besides. ;) – Andrew Thompson Apr 23 '12 at 12:45
  • and I prefer ... SSCCE (pip pip pip talking master of shots to the dark) +1 for both – mKorbel Apr 23 '12 at 12:48
  • @mKorbel I was just thinking of your (generally better, I think) examples of using renderers for a variety of components. :) – Andrew Thompson Apr 23 '12 at 12:52
  • @Andrew Thompson not, you sent great answer, but I'd bet that OP casting value from JList, JComboBox or some another whatever Model :-), maybe why casting ??? – mKorbel Apr 23 '12 at 13:00
2

All it means is that Restaurant.Time doesn't override the toString() method, so the default implementation provided by Object is used.

If you want the output to look differently, you'll need to override Restaurant.Time.toString().

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • It actually does, though the default `Object#toString()`, however it doesn't override it with a more meaningful implementation. But this is nitpicking, still +1'd. – Romain Apr 23 '12 at 12:31