Solved

Jlist

Posted on 2009-05-11
17
272 Views
Last Modified: 2012-05-06
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
Comment
Question by:valleytech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24359864
Object item = jList.getModel().getElementAt(0);
String label = item.toString();

0
 

Author Comment

by:valleytech
ID: 24359880
i am late one step Objects
 softwareJList.setSelectedIndex(0);
inputSoftwareText = softwareJList.getSelectedValue().toString();
                              
0
 

Author Comment

by:valleytech
ID: 24359908
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 92

Expert Comment

by:objects
ID: 24359912
>  softwareJList.setSelectedIndex(0);

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


0
 
LVL 92

Expert Comment

by:objects
ID: 24359935
the code i posted above does not rely on the seledtion , it just returns the first element

0
 

Author Comment

by:valleytech
ID: 24359949
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
 
LVL 92

Expert Comment

by:objects
ID: 24359980
jList.ensureIndexIsVisible(0);

0
 

Author Comment

by:valleytech
ID: 24360035
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
 
LVL 92

Expert Comment

by:objects
ID: 24360046
>       + 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
 

Author Comment

by:valleytech
ID: 24360072
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
 
LVL 92

Expert Comment

by:objects
ID: 24360109
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
 

Author Comment

by:valleytech
ID: 24360112
I see. So i better change to combobox?
0
 

Author Comment

by:valleytech
ID: 24360121
before i change to combobox, i just wonder whether i can display only 1 item at a time in comboBox?
0
 
LVL 92

Expert Comment

by:objects
ID: 24360141
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
 

Author Comment

by:valleytech
ID: 24360200
and same problem. How to get first item if user is lazy to select item within combo box> thanks a lot.
0
 
LVL 92

Expert Comment

by:objects
ID: 24360222
they cannot be lazy with a combo, only the selected item will be displayed

0
 

Author Comment

by:valleytech
ID: 24360243
cool. let me close this. please help me on GUI question.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question