setSelectedValue

Discussion in 'Java' started by Abs, Jun 5, 2004.

  1. Abs

    Abs Guest

    Hi!

    I'm using the "setSelectedValue" method from the JList class to
    select an item from a list of objects extending a class I developed
    named "Transition". This is the code I cannot get to work:

    DefaultListModel model=new DefaultListModel();
    JList list=new JList(model);

    model.addElement(new DefaultTransition());
    model.addElement(new FadeTransition());

    Transition transition=getTransition();
    list.setSelectedValue(transition,true);

    I supposed the "setSelectedValue" method would select one of the two
    items in the list but nothing gets selected. What am I doing wrong ?


    Thanks in advance

    --
    Abs
    Abs, Jun 5, 2004
    #1
    1. Advertising

  2. Abs

    Filip Larsen Guest

    Abs wrote

    > I'm using the "setSelectedValue" method from the JList class to
    > select an item from a list of objects extending a class I developed
    > named "Transition".


    JList uses the equals method on your elements to find the index of the
    element you specify in the call to setSelectedValue. So, either give
    your elements a proper equals semantic [define a boolean equals(Object)
    and int hashCode() method] or pass a reference to an element that is in
    the model.

    However, you may find it easier to simply use JList.setSelectedIndex
    instead.


    Regards,
    --
    Filip Larsen
    Filip Larsen, Jun 6, 2004
    #2
    1. Advertising

  3. Abs

    Abs Guest

    Filip Larsen wrote:
    > Abs wrote
    >
    >
    >> I'm using the "setSelectedValue" method from the JList class to
    >>select an item from a list of objects extending a class I developed
    >>named "Transition".

    >
    >
    > JList uses the equals method on your elements to find the index of the
    > element you specify in the call to setSelectedValue. So, either give
    > your elements a proper equals semantic [define a boolean equals(Object)
    > and int hashCode() method] or pass a reference to an element that is in
    > the model.
    >
    > However, you may find it easier to simply use JList.setSelectedIndex
    > instead.
    >
    >
    > Regards,


    thanks for the info
    Abs, Jun 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page