Solved

key-search in JList Component

Posted on 2002-07-22
11
277 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
Industry Leaders: 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!

 

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

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 74
java example issue 5 44
What is the use of Forwarding Class in java 1 37
Java pass by reference 3 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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