Carsten Schäfer wrote:
> I sometimes see the following exception:
> java.lang.IllegalArgumentException: Invalid location
/ ...
> when i call setSelectedIndex(0) on a combobox filled with some strings.
>
> Does somebody know what it means ?
Yes, it means there is no item with an index of 0. Solution: test for the
presence of this indexed item before trying to select it.
Remember, in programming, zero is a valid number just like one or two, and
if you specify an index of zero, there must be an index-zero item present,
or you will get an error.

Signature
Paul Lutus
http://www.arachnoid.com
Carsten Sch?fer - 23 Sep 2004 17:00 GMT
>> I sometimes see the following exception:
>> java.lang.IllegalArgumentException: Invalid location
[quoted text clipped - 12 lines]
> two, and if you specify an index of zero, there must be an index-zero
> item present, or you will get an error.
I don't understand your answer.
I thought setSelectedIndex(0) selects the first item in a JComboBox.
If there are no items in the combobox i get: setSelectedIndex: 0 out of
bounds
How do i test the presence of the item at index 0 ?
Carsten
Thomas Fritsch - 23 Sep 2004 17:31 GMT
Carsten Schäfer schrieb:
>>Carsten Schäfer wrote:
>>
[quoted text clipped - 21 lines]
>
> How do i test the presence of the item at index 0 ?
probably like this:
if (comboBox.getItemCount() > 0)
comboBox.setSelectedIndex(0);
> Carsten
>

Signature
Thomas<dot>Fritsch<squiggle>ops<dot>de
Carsten Sch?fer - 23 Sep 2004 17:16 GMT
> Carsten Sch?fer schrieb:
>>
[quoted text clipped - 25 lines]
> if (comboBox.getItemCount() > 0)
> comboBox.setSelectedIndex(0);
This tests if there are items in the combobox.
As i wrote i get 'java.lang.IllegalArgumentException: Invalid location'
and not 'setSelectedIndex: 0 out of bounds'
This would happen if there are no items in the combobox.
Or am i wrong ?
Carsten
Paul Lutus - 23 Sep 2004 17:47 GMT
Carsten Schäfer wrote:
/ ...
>> probably like this:
>> if (comboBox.getItemCount() > 0)
[quoted text clipped - 5 lines]
> This would happen if there are no items in the combobox.
> Or am i wrong ?
Why not take the suggestion and test for the presence of an item at index
zero? See if this solves the problem.
Also, it is possible to put an "object" equal to null at index zero. This
might result in the error message you saw. We really cannot say without
seeing your code.

Signature
Paul Lutus
http://www.arachnoid.com
Babu Kalakrishnan - 24 Sep 2004 06:31 GMT
> Carsten Schäfer wrote:
>
[quoted text clipped - 16 lines]
> might result in the error message you saw. We really cannot say without
> seeing your code.
It would solve the problem only if it were an ArrayOutOfBoundsException
that he got.
If you looked at the stacktrace carefully, you would notice that the
exception was generated when swing tried to update the Caret in the
textbox portion - so there is no point trying to solve a non-existent issue.
To the OP : Most errors of this type occur if the thread safety rules of
the swing framework are not followed. So check your code to see if you
are making this call from some thread other than the EDT. (it certainly
appears to be that if what you posted is the *complete* stacktrace.
Stacktraces originating from code running in the EDT should have some
event queue processing code as the last lines)
BK
Thomas Fritsch - 23 Sep 2004 18:33 GMT
Carsten Schäfer schrieb:
>>Carsten Schäfer schrieb:
>>
[quoted text clipped - 35 lines]
>
> Carsten
Hmpf, you are right!
After re-reading the exception stack trace in your original post I saw:
The exception did *not* occur because 0 were an illegal combobox item
index, but because there was an illegal *character* index in the string
when JTextComponent#getSelectedText fiddled around with selected text
and caret position.
=> it may be a bug of yours or of Sun...
Consider posting a stripped down version of your source.

Signature
Thomas<dot>Fritsch<squiggle>ops<dot>de