Dynamic Search Box

A dynamic search box key press function callback for an edit box
Updated 6 Oct 2011

View License

KPF_CB was completely rewritten to work with mouse clicks and for Matlab R2011b. Currently it does not work with other releases. I haven't really looked into why yet, so I have included the original 'KPF_CB' and renamed it 'Original_KPF_CB' for compatibility.

The new KPF_CB also heavily relies on Java. The Java part was made possible by Yair Altman and his 'findjobj'.

From the help file,

% a key press callback function for an edit box
% Description
% KPF_CB() is a key press callback function for an edit box. This
% callback creates a dynamically changing listbox under or above the
% edit box (depending on space available). The contents of the listbox
% change depending on the keys pressed. This callback may be useful
% where very large lists are encountered and are difficult to sort
% through, or may act as a search box.
% Syntax
% set(edit_h,'KeyPressFcn', ...
% {@KPF_CB,jedit_h,slider_h,fig_h,list,func})
% Arguments
% edit_h - handle of the edit box to which KPF_CB is applied
% jedit_h - handle of the java component of the edit box
% slider_h - handle of the slider, if none, leave empty []
% fig_h - handle of the figure of the edit box
% list - list of selectable items
% func - function to evaluate after 'return', or 'escape' key is
% pressed or item in listbox is 'opened', if this is not needed,
% leave empty [] or do not include in edit box 'KeyPressFcn'
% callback
% Other Functionality
% - KPF_CB incorporates the functionality of many special keys
% including, backspace, delete, escape, insert, end, shift, return,
% arrow keys, and space.
% - Arrow keys allow the user to cycle left to right across the edit
% box and continue typing with a dynamically changing listbox. They
% also allow the user to scroll through the list box up and down.
% - The 'return' key will either select the closest match to the
% contents of the listbox from the contents of the edit box, or if
% the arrow keys were previously used to cycle through the listbox,
% then whichever item is currently highlighted will be selected. This
% selected word will then appear in the edit box, and the listbox will
% be deleted.
% - Other keys behave as expected
% - If an item in the listbox is clicked, the edit box will display
% the item clicked on, but the contents of the listbox will remain the
% same. If an item in the listbox is opened (i.e. double-clicked),
% this will have the same effect as the 'return' key, but for the item
% clicked on.
% - A slider can be used with this function. For example, if multiple
% edit boxes were placed on a panel with a slider, the listbox would
% change size depending on the position of the edit box and slider.
% - The optional function to evaluate after 'return', or 'escape' key
% is pressed or item in listbox is 'opened' allows for immediate
% function evaluation without needing a 'push button'
% Example
% list = cell(26,26,26);
% for i1 = 1:26
% for i2 = 1:26
% for i3 = 1:26
% list{i1,i2,i3} = [char(64+i1) char(64+i2) char(64+i3)];
% end
% end
% end
% list = reshape(list,numel(list),1);
% scrnsz = get(0,'ScreenSize');
% fig_h = figure('MenuBar','none','Resize','off', ...
% 'Position',[scrnsz(3)/2-100 scrnsz(4)/2-100 200 200]);
% edit_h = uicontrol('Style','edit','Position',[6 170 190 25]);
% jedit = java(findjobj(edit_h));
% jedit_h = handle(jedit,'CallbackProperties');
% set(edit_h,'KeyPressFcn',{@KPF_CB,jedit_h,[],fig_h,list,@my_fun})
% set(jedit_h,'MouseReleasedCallback',{@KPF_MR_CB,edit_h}, ...
% 'MousePressedCallback',{@KPF_MP_CB,edit_h})
% function my_fun
% get(edit_h,'String')
% end
% Notes
% - In order to close the listbox an item must be selected, by either
% pressing either the 'escape' or 'return' keys or 'opening' an item in
% the listbox
% - This function makes use of the edit box user data variable
% - Java is used extensively
% - This function is not bug free yet, but usually hitting 'backspace'
% or 'delete' enough times will clear whatever my function thinks is in
% there

% Version : 1.1 (10/06/2011)
% Author : Nate Jensen
% Created : 09/21/2011
% History :
% - v1.0 (09/21/2011) : initial release
% - v1.1 (10/06/2011) : added java component to enable mouse clicks

Cite As

Nate Jensen (2024). Dynamic Search Box (https://www.mathworks.com/matlabcentral/fileexchange/32971-dynamic-search-box), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Migrate GUIDE Apps in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

completely rewrote code, added more comments, added java component to capture mouse clicks, does not work properly in other versions yet, renamed submission

v1.01 hot fix