setSelectedValue

A

Abs

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
 
F

Filip Larsen

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,
 
A

Abs

Filip said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top