Solved

Jlist

Posted on 2009-05-11
17
267 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 75
count7 challenge 12 67
countX 22 69
wordlen challenge 3 46
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now