Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jlist

Posted on 2009-05-11
17
Medium Priority
?
275 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

718 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