Skip to main content

list.getSelectedIndex

2 replies [Last post]
Anonymous

list.getSelectedIndex returns 0 for an empty list, shouldn't this be
-1 and this should be indicated in the javadoc!

see: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/List.html#getSelectedIndex()

this also causes problem for getSelectedItem because it will request
object at index 0 which does not exist in the model, this method
should check if the index == -1 and return null if it is the case

--
http://www.somatik.be
Microsoft gives you windows, Linux gives you the whole house.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Shai Almog

Hi,
I corrected getSelectedItem() to return null for such a case. This is
pretty easy to distinguish since size is bigger than the index.
We chose not to go with the -1 convention since it has some edge
cases when we actually do add content to the list and expect the
first element to be selected. Unlike the Swing version of List we
have somewhat different constraints.

Thanks,
Shai.

> list.getSelectedIndex returns 0 for an empty list, shouldn't this be
> -1 and this should be indicated in the javadoc!
>
> see: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/
> List.html#getSelectedIndex()
>
> this also causes problem for getSelectedItem because it will request
> object at index 0 which does not exist in the model, this method
> should check if the index == -1 and return null if it is the case
>
> --
> http://www.somatik.be
> Microsoft gives you windows, Linux gives you the whole house.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]

Francis De Brabandere

if it's indicated in the javadoc what will be returned when the list
is empty then this shouldn't be a problem

On Wed, Jul 30, 2008 at 12:55 PM, Shai Almog wrote:
> Hi,
> I corrected getSelectedItem() to return null for such a case. This is pretty
> easy to distinguish since size is bigger than the index.
> We chose not to go with the -1 convention since it has some edge cases when
> we actually do add content to the list and expect the first element to be
> selected. Unlike the Swing version of List we have somewhat different
> constraints.
> Thanks,
> Shai.
>
> list.getSelectedIndex returns 0 for an empty list, shouldn't this be
> -1 and this should be indicated in the javadoc!
> see:
> http://java.sun.com/j2se/1.5.0/docs/api/java/awt/List.html#getSelectedIndex()
> this also causes problem for getSelectedItem because it will request
> object at index 0 which does not exist in the model, this method
> should check if the index == -1 and return null if it is the case
> --
> http://www.somatik.be
> Microsoft gives you windows, Linux gives you the whole house.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
> Shai Almog
> http://lwuit.blogspot.com/
>

--
http://www.somatik.be
Microsoft gives you windows, Linux gives you the whole house.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net