• 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

JComboBox Model Help

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have absouletely no clue what I am doing wrong. This is my first attempt at using JComboBox model, and I must have missed something because the list is blank even though a test System.out.println successfully returned String objects for getCourseName()'s. here's my ComboBoxModel code...

And in other part of my code:
courseList = new JComboBox(new CourseComboBoxModel(courses));
where courses is a Course[] array.
Any ideas what I'm doing wrong?
Many thanks,
Craig
------------------
boolean isDevilish(Human h)
{
if(h==CraigFlannagan)
return true;
else return false;
}
[This message has been edited by Craig Flannagan (edited November 14, 2001).]
[This message has been edited by Craig Flannagan (edited November 14, 2001).]
[This message has been edited by Craig Flannagan (edited November 14, 2001).]
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say your list is blank, does it means that the text field of the combo box is empty or that the drop down list is empty?
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

</BLOCKQUOTE>
Here you should not return courses[idx].getCourseName() but courses[idx]. Indeed when the list request the object at the position idx, you are not returning the object but a String...
Moreover you should provide a toString method in the Course object so that the name could be displayed in the Combo.
At last, when instantiating the ComboBox, you should call the setSelectedItem method of the JComboBox so that the text field is not empty.
W.

[This message has been edited by Wilfried LAURENT (edited November 14, 2001).]
 
Craig Flannagan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still showing an empty drop-down list, while the courses array isn't empty.
I did make the toString() method. So how does the JVM know to use that, if the data model constructor uses Object objects as parameter?
Thanks,
Craig
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig,
I copied your code and created a Course class and the combo gets filled fine. What does your toString() look like?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the following code, I got it to work...



Hope this helps,
-Nate

BTW, toString() is a method of Object, so everything inherits that method. It was used in the data model for that reason... anything created will have that method, so anything you send to it will display something. ( It may not make sense... but at least it will display, and you won't get exceptions... )
 
Craig Flannagan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks. I will compare the code and see what I missed.
Craig
------------------
boolean isDevilish(Human h)
{
if(h==CraigFlannagan)
return true;
else return false;
}
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nathan Pruett:
[B]


Isn't that a pleonasm?


BTW, toString() is a method of Object, so everything inherits that method. It was used in the data model for that reason... anything created will have that method, so anything you send to it will display something. ( It may not make sense... but at least it will display, and you won't get exceptions... )[/B]


If you do not override the toString method, then your will have the reference -something like "yourpackage.Course@120012"-displayed in the text field.
W.
[This message has been edited by Wilfried LAURENT (edited November 15, 2001).]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wilfried LAURENT:

Originally posted by Nathan Pruett:


Isn't that a pleonasm?



Yes... it's also a tautology...

-Nate
 
reply
    Bookmark Topic Watch Topic
  • New Topic