How to create search functionality in a ListBox

This listbox is quite long so a search functionality is required.Thanks in advance.
[104 byte] By [tnr009] at [2007-9-26 4:21:09]
# 1

You can add a textfield above the list to accept the search string.

Once you have that, you can have 2 options:

1. Add a key listener for the textfield. The "keypressed" event in the textfield would have to invoke the searching in the list.

2. Let the user enter the search string and hit a search button or <ENTER> in the textfield. This action would invoke the searching in the textfield.

The searching part is straightforward. Go through the elements in the list and compare them with the search string in the textfield. To increase efficiency, you may want to apply efficient search algorithms (may be have the list elements sorted at the first place when you initially populated the list.). Once you find the list entry, you can scroll the scrollpane holding the list to show the particular entry. Take a look at this:

http://forum.java.sun.com/thread.jsp?forum=57&thread=161273

Let me know if you need a code sample.

Hope that helps.

Regards,

Vijay

vijayreddy at 2007-6-29 17:25:05 > top of Java-index,Archived Forums,Swing...
# 2
Vijay,Can you please send me some code sample?Thanks in advance.mhastee@hotmail.com
tnr009 at 2007-6-29 17:25:05 > top of Java-index,Archived Forums,Swing...