Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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.
0
valleytech
Asked:
valleytech
  • 9
  • 8
1 Solution
 
objectsCommented:
Object item = jList.getModel().getElementAt(0);
String label = item.toString();

0
 
valleytechAuthor Commented:
i am late one step Objects
 softwareJList.setSelectedIndex(0);
inputSoftwareText = softwareJList.getSelectedValue().toString();
                              
0
 
valleytechAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
>  softwareJList.setSelectedIndex(0);

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


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

0
 
valleytechAuthor Commented:
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.
0
 
objectsCommented:
jList.ensureIndexIsVisible(0);

0
 
valleytechAuthor Commented:
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.
0
 
objectsCommented:
>       + 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?



0
 
valleytechAuthor Commented:
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.
0
 
objectsCommented:
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
0
 
valleytechAuthor Commented:
I see. So i better change to combobox?
0
 
valleytechAuthor Commented:
before i change to combobox, i just wonder whether i can display only 1 item at a time in comboBox?
0
 
objectsCommented:
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
0
 
valleytechAuthor Commented:
and same problem. How to get first item if user is lazy to select item within combo box> thanks a lot.
0
 
objectsCommented:
they cannot be lazy with a combo, only the selected item will be displayed

0
 
valleytechAuthor Commented:
cool. let me close this. please help me on GUI question.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now