Solved

key-search in JList Component

Posted on 2002-07-22
11
275 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
  • 5
  • 4
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 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