Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes JComboBox Model Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox Model Help" Watch "JComboBox Model Help" New topic
Author

JComboBox Model Help

Craig Flannagan
Greenhorn

Joined: Oct 21, 2001
Posts: 10
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).]


<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />boolean isDevilish(Human h)<br />{<br /> return (h==CraigFlannagan);<br />}<br /></font></pre><hr></blockquote>
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
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

Joined: Jul 13, 2001
Posts: 269

</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

Joined: Oct 21, 2001
Posts: 10
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Oct 18, 2000
Posts: 4121

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... )


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Craig Flannagan
Greenhorn

Joined: Oct 21, 2001
Posts: 10
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

Joined: Jul 13, 2001
Posts: 269
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

Joined: Oct 18, 2000
Posts: 4121

Originally posted by Wilfried LAURENT:
Originally posted by Nathan Pruett:


Isn't that a pleonasm?


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

-Nate
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JComboBox Model Help