Solved

Jlist

Posted on 2009-05-11
17
269 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 42
servlet example issue 6 40
how to see all occupied ports on windows 10 laptop 15 66
eclipse console opening separately 2 21
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

803 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