Solved

Jlist

Posted on 2009-05-11
17
274 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

623 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