Link to home
Start Free TrialLog in
Avatar of valleytech
valleytechFlag for United States of America

asked on

Jlist

when user select one item on JList, I can obtain selection by:
String input= jList.getSelectedValue().toString();
However, I want to get the first item on the Jlist if user doesn't
make any selection. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of valleytech

ASKER

i am late one step Objects
 softwareJList.setSelectedIndex(0);
inputSoftwareText = softwareJList.getSelectedValue().toString();
                              
how can it can't take the value when i click on scrollpane without clicking on the text inside Jlist?? The Jlist display only one row at a time.
>  softwareJList.setSelectedIndex(0);

you would only do that if you actually wanted to select the first item


the code i posted above does not rely on the seledtion , it just returns the first element

so please help me on select the item either user click on item or just scroll down. JList can display one row at a time.
jList.ensureIndexIsVisible(0);

My Jlist has a scrollpane and can display one row at a time.
when user doesn't do anything, it will select first item of the list. Work perfectly.
However, when user roll down scrollpane to  make a selection on JList:
      + if he click on the item. Work perfectly
      + if he is lazy to click on item, DOESN't work.
Please help.
>       + if he is lazy to click on item, DOESN't work.

So you want to select the visible item even if they don't click. Is that correct?
When would it change the selection?

Why not use a JComboBox instead?



yeSo you want to select the visible item even if they don't click. Is that correct?  <-- that's true.
 I start by jlist so i get stuck with it. hic hic.
cause the user may just have scrolled down to look at other options available and does not want to select the visible item.
What you are trying to do sort of breaks ui guidelines
I see. So i better change to combobox?
before i change to combobox, i just wonder whether i can display only 1 item at a time in comboBox?
a combo only display ones at a time, it uses a pull down to change the displayed item
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
and same problem. How to get first item if user is lazy to select item within combo box> thanks a lot.
they cannot be lazy with a combo, only the selected item will be displayed

cool. let me close this. please help me on GUI question.