Solved

key-search in JList Component

Posted on 2002-07-22
11
281 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 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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