Click to See Complete Forum and Search --> : JComboBox -- Item is not in the list[Resolved]
System_Error
Jul 6th, 2005, 09:57 AM
I have a "dynamic" JComboBox. What I mean by that is items are removed and added on "the fly".
The getSelectedIndex() is returning -1 when an item is selected. I looked this up and it indicates the selected item is not in the list....What in the world does that mean? The item was added to the ComboBox.
One more thing, the getItemCount() does return the correct number of items..So I don't know why I can't get the selected item or get the selected index.
So, can anyone help me with this?
By the way, I've tried adding a revalidate call but that didn't work.
NoteMe
Jul 6th, 2005, 10:05 AM
Can you show the code where you are adding the elements, and the code for when you are trying to fetch the index?
- ии -
System_Error
Jul 6th, 2005, 10:43 AM
It will be hard because I'm using snippets everywhere. This program is breaching 1200 lines, so I'll do my best.
Adding Items....This method is called when there is a change in one of the sliders I have:
public void addItemsToMenu()
{
cbStartMonth.removeAllItems();
cbStartMonth.addItem("<optional>");
int startYear = startYearSlider.getValue();
int endYear = endYearSlider.getValue();
for (int i=startYear; i< endYear; i++)
{
cbStartMonth.addItem(startYear);
startYear++;
}
cbStartMonth.revalidate();
}
Then, I call the other combobox's addItem method based on what was selected in the first combobox, BUT it returns a -1 for the index of the selected item.
if (ae.getSource() == cbStartMonth)
{
try
{
if (((int)cbStartMonth.getSelectedIndex()) != 0 && cbStartMonth.getItemCount() > 1)
{
int x = Integer.parseInt(((String)cbStartMonth.getSelectedItem()));
addItemsToEndYearMenu((x));
cbEndYear.setEnabled(true);
}
}
catch (Exception e)
{
e.printStackTrace();
cbEndYear.setEnabled(false);
}
}
I know none of that probably made any sense!
Basically to see what was happening I added System.out.println()'s to everything. That is where I found out that it was returning -1 and that meant the item selected was not in the list:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#getSelectedIndex()
That might explain it a bit better.
System_Error
Jul 6th, 2005, 10:56 AM
I should also add, at this line it's giving me a classcast exception:
int x = Integer.parseInt(((String)cbStartMonth.getSelectedItem()));
I don't understant that though. I'm casting it to an int?
Dillinger4
Jul 6th, 2005, 06:49 PM
System_Error i noticed that you have int startYear = startYearSlider.getValue(); but then you have cbStartMonth.addItem(startYear); i thought addItem() takes an Object as an arg?
System_Error
Jul 6th, 2005, 06:59 PM
An int is of type Object so that does work. Anyways, I decided to go down a different road, one that looks and performs much better. I do need some help with a few things, so if your willing please look at the other thread I've created.
Guys, thank you for your help. I'm sorry I up and changed my mind, but this wasn't working out very well. Anyways, thank you both again for offering to help.
NoteMe
Jul 7th, 2005, 03:09 AM
Hmm...hard to guess here. Havn't had the time to make a swing app to test it.
But try to split up the line:
int x = Integer.parseInt(((String)cbStartMonth.getSelectedItem()));
into at least two lines, and see what you get printed out then. Is it the x here you say is "-1" or is it the getSelectedIndex() that returns "-1", if it is the X, maybe try to use trim() on the string before casting it or something like that.
And if you manage to recreate the whole scenary in a minimum file, it would be nice if you posted it, so I could have the time to test it. Right now I have to much work to do, so I don't have the time to write it all my self.
[Edit]Ooops, didn't see your last post..:D..shame on me for not reading all the posts before trying to help..:D
- ии -
NoteMe
Jul 7th, 2005, 03:54 AM
Grrr....the other stuff I was doing was so boring, that I wanted to do this in stead. Have just installed NetBeans in Debian, so had to test it out. The forms and Comboboxes are soooooo cool looking in Debian..:D.
Well, Dilenger was in the right direction of the problem. Change this line:
cbStartMonth.addItem(startYear);
to this, and you are ready to go.
cbStartMonth.addItem(Integer.toString(startYear));
I guess if you case it to object you would be even safer, but this should work. At least it works for me in java 5 here.
- ии -
System_Error
Jul 7th, 2005, 10:41 AM
Alirght, I thought I had the problem fixed but I was wrong. I switched over to using JList which I like better but it's still giving me a ClassCastException on that one line.
I'm going to try what you and Dillenger are telling me and then get back to you. I'm thinking that will do it.
System_Error
Jul 7th, 2005, 10:47 AM
It turns out that worked. And also splitting up that one liner also worked. Thanks guys for helping me, it's much appreciated.
System_Error
Jul 7th, 2005, 10:48 AM
Just to let you know, I can't give either of you rep..It says I must spread it around first. :(
Dillinger4
Jul 7th, 2005, 01:20 PM
Posted by System_Error
An int is of type Object so that does work.
I think it would be if you were doing this in C#. Im pretty sure they have what is called a unified type system where everything is treated an as object.
Yeah don't worry about the rep points i could care less about them. I try and do the same thing also and i get the same message telling me that i have to spread them around first.
System_Error
Jul 7th, 2005, 01:27 PM
Well, what you said worked though. I would have thought since an int is virtually an object what I had should have worked, but didn't.
Basically, I stored it in an object first:
Object o = JListName.getSelectedValue();
String selection = o.toString();
int value = Integer.parseInt(selection);
I think that top line was the key.
NoteMe
Jul 7th, 2005, 01:29 PM
Yeah don't worry about the rep points i could care less about them. I try and do the same thing also and i get the same message telling me that i have to spread them around first.
Totaly confusing when you can't give a rep. But I don't want others to belive that I don't appreciate their help so I usualy bookmark the posts, so I can get back to it. The spread is really not ment for small sections like this and the game section.
- ии -
NoteMe
Jul 7th, 2005, 01:32 PM
Well, what you said worked though. I would have thought since an int is virtually an object what I had should have worked, but didn't.
Basically, I stored it in an object first:
Object o = JListName.getSelectedValue();
String selection = o.toString();
int value = Integer.parseInt(selection);
I think that top line was the key.
Would have guessed so, but it isn't nessesary though, since you can do it in one line like I showed you.
- ии -
Dillinger4
Jul 7th, 2005, 01:33 PM
NoteMe i brought this problem up in the feedback forum. :thumb:
NoteMe
Jul 7th, 2005, 01:37 PM
Yeah I saw it. Been brought up a "few" times before, so I guess one of the eager youn mods are going to point you to one of them and say that it won't be changed. The spread used to be 25, now it is down to 10. But I still feel that it is way to high. I mod 2 small sections, and rarely ask questions, but when I do it is the same 4 persons answering every time. That means that 60% of the reps I am giving out is to other random posts...grrr....sorry for ranting in this thread, but no where else to do it. No ohter mods will see it here...:D
- ии -
Dillinger4
Jul 7th, 2005, 01:38 PM
Can use cbStartMonth.addItem(String.valueOf(startYear)); also
System_Error
Jul 7th, 2005, 01:42 PM
but when I do it is the same 4 persons answering every time.
- ии -
Same here. I don't post much, but when I do it's only in the java forum where about 3 people are answering my questions.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.