Solved

key-search in JList Component

Posted on 2002-07-22
11
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:bijansengupta
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:bijansengupta
Comment Utility
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
Comment Utility
To find the first element that starts with 'a':

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

Author Comment

by:bijansengupta
Comment Utility
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
Comment Utility
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
Comment Utility
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

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.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 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

10 Experts available now in Live!

Get 1:1 Help Now