Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox Model Help

 
Craig Flannagan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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).]
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • 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?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic