?
Solved

key-search in JList Component

Posted on 2002-07-22
11
Medium Priority
?
288 Views
Last Modified: 2012-05-04
Hi

I have a JList which actually gets populated from a vector
Now I want to let an item automatically selected once user presses any letter from the keyboard on the JList. It must be first item  selected from the list starting with that letter. It seems it is easy to focus on any item on the list using
list.setSelectedIndex(int index);

but there is no way to pass a char to select item from the list.

I need a urgent solution.

thanks in advance


bijan
0
Comment
Question by:bijansengupta
[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
  • 5
  • 4
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 7170970
There is no inbuilt support in JList for doing that.
Easy enough to implement though, you just have to determine the required index by searching thru the lists contents for the matching element or maintaining some form of lookup table.
0
 

Author Comment

by:bijansengupta
ID: 7172886
Hi

kindly post a sample code for this. It will be great help for me. Try to avoid loop if possible to get the index.

My Code(This is how I am creating the JList)
--------

MyBean beanObj1 = new MyBean();
beanObj1.setId("1001");
beanObj1.setText("text1");

MyBean beanObj2 = new MyBean();
beanObj2.setId("1002");
beanObj2.setText("text2");
...

Vector vec = new Vector();
vec.addElement(beanObj1);
vec.addElement(beanObj2);
...

JList list = new JList();
list.setListData(vec);

thanks again for your valuable suggestion

bijan



0
 
LVL 92

Expert Comment

by:objects
ID: 7173163
MyBean beanObj1 = new MyBean();
beanObj1.setId("1001");
beanObj1.setText("text1");

MyBean beanObj2 = new MyBean();
beanObj2.setId("1002");
beanObj2.setText("text2");
...

Vector vec = new Vector();
vec.addElement(beanObj1);
vec.addElement(beanObj2);
...

int[] lut = new int[26];
Arrays.fill(lut, -1);
lut[0] = ...    // 'a'
lut[1] = ...    // 'b'

JList list = new JList();
list.setListData(vec);
...

int index = lut[charEntered - 'a'];
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:bijansengupta
ID: 7175896
this portion of your code is not very much clear

int[] lut = new int[26];
Arrays.fill(lut, -1);
lut[0] = ...    // 'a'
lut[1] = ...    // 'b'

what should I pass as a value to

lut[0] = ...    // 'a'
lut[1] = ...    // 'b'

can you please make it more clear.

thanks

bijan

0
 
LVL 92

Expert Comment

by:objects
ID: 7175913
Sorry the values you need are the index you'd like selected if the corresponding char key is pressed.
ie. lut[0] would contain the index to select when 'a' was pressed, lut[1] would contain the index to select when 'b' was pressed, etc.
0
 

Author Comment

by:bijansengupta
ID: 7215591
Well JDK 1.4.1 has a method in JList class
 getNextMatch(String prefix, int startIndex, Position.Bias bias)  which return a int position but I really cannot figure how to use it.

If you can please do let me know this will really help and I can then give these points.

Thanx in advance.
0
 
LVL 92

Expert Comment

by:objects
ID: 7215916
To find the first element that starts with 'a':

int index = list.getNextMatch("a", 0, Position.Bias.Forward);
0
 

Author Comment

by:bijansengupta
ID: 7217576
That sounds good but how do I actually use this method I mean tell me the implementation from scratch ie how do I use the KeyListener and then eventually how to I go Forward and Backward dynmically.

If you can post the code from scratch from making a JList and then impelmenting the key search it will really be helpful.

I am increasing the points to 100.

Thanx in advance.
0
 
LVL 92

Expert Comment

by:objects
ID: 7218648
In your key listener get the character pressed then:

int index = list.getNextMatch(pressed, 0, Position.Bias.Forward);
setSelectedIndex(index);
0
 
LVL 35

Expert Comment

by:girionis
ID: 8951671
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to  objects

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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