- Undocumented Matlab - https://undocumentedmatlab.com -
Adding a search box to figure toolbar
Posted By Yair Altman On March 30, 2016 | 3 Comments
Last week I wrote about my upcoming presentations [1] in Tel Aviv and Munich, where I will discuss a Matlab-based financial application that uses some advanced GUI concepts. In today’s post I will review one of these concepts that could be useful in a wide range of Matlab applications – adding an interactive search box to the toolbar of Matlab figures.
The basic idea is simple: whenever the user types in the search box, a Matlab callback function checks the data for the search term. If one or more matches are found then the searchbox’s background remains white, otherwise it is colored yellow to highlight the term. When the user presses <Enter>, the search action is triggered to highlight the term in the data, and any subsequent press of <Enter> will highlight the next match (cycling back at the top as needed). Very simple and intuitive:
The pieces of today’s post were already discussed separately on this website, but never shown together as I will do today:
com.mathworks.widgets.SearchTextField
) was discussed in last year’s article on auto-completion widgets [2]As a first step, let’s create the search-box component and add it to our figure’s toolbar:
% First, create the search-box component on the EDT, complete with invokable Matlab callbacks:
jSearch = com.mathworks.widgets.SearchTextField('Symbol'); % 'Symbol' is my default search prompt
jSearchPanel = javaObjectEDT(jSearch.getComponent); % this is a com.mathworks.mwswing.MJPanel object
jSearchPanel = handle(jSearchPanel, 'CallbackProperties'); % enable Matlab callbacks
% Now, set a fixed size for this component so that it does not resize when the figure resizes:
jSize = java.awt.Dimension(100,25); % 100px wide, 25px tall
jSearchPanel.setMaximumSize(jSize)
jSearchPanel.setMinimumSize(jSize)
jSearchPanel.setPreferredSize(jSize)
jSearchPanel.setSize(jSize)
% Now, attach the Matlab callback function to search box events (key-clicks, Enter, and icon clicks):
jSearchBox = handle(javaObjectEDT(jSearchPanel.getComponent(0)), 'CallbackProperties');
set(jSearchBox, 'ActionPerformedCallback', {@searchSymbol,hFig,jSearchBox})
set(jSearchBox, 'KeyPressedCallback', {@searchSymbol,hFig,jSearchBox})
jClearButton = handle(javaObjectEDT(jSearchPanel.getComponent(1)), 'CallbackProperties');
set(jClearButton, 'ActionPerformedCallback', {@searchSymbol,hFig,jSearchBox})
% Now, get the handle for the figure's toolbar:
hToolbar = findall(hFig,'tag','FigureToolBar');
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer'); % or: hToolbar.JavaContainer.getComponentPeer
% Now, justify the search-box to the right of the toolbar using an invisible filler control
% (first add the filler control to the toolbar, then the search-box control):
jFiller = javax.swing.Box.createHorizontalGlue; % this is a javax.swing.Box$Filler object
jToolbar.add(jFiller, jToolbar.getComponentCount);
jToolbar.add(jSearchPanel, jToolbar.getComponentCount);
% Finally, refresh the toolbar so that the new control is displayed:
jToolbar.revalidate
jToolbar.repaint
Now that the control is displayed in the toolbar, let’s define what our Matlab callback function searchSymbol() does. Remember that this callback function is invoked whenever any of the possible events occur: keypress, <Enter>, or clicking the search-box’s icon (typically the “x” icon, to clear the search term).
We first reset the search-box appearance (foreground/background colors), then we check the search term (if non-empty). Based on the selected tab, we search the corresponding data table’s symbol column(s) for the search term. If no match is found, we highlight the search term by setting the search-box’s text to be red over yellow. Otherwise, we change the table’s selected row to the next match’s row index (i.e., the row following the table’s currently-selected row, cycling back at the top of the table if no match is found lower in the table).
Reading and updating the table’s selected row requires using my findjobj utility [5] – for performance considerations the jTable handle should be cached (perhaps in the hTable’s UserData or ApplicationData):
% Callback function to search for a symbol
function searchSymbol(hObject, eventData, hFig, jSearchBox)
try
% Clear search-box formatting
jSearchBox.setBackground(java.awt.Color.white)
jSearchBox.setForeground(java.awt.Color.black)
jSearchBox.setSelectedTextColor(java.awt.Color.black)
jSearchBox.repaint
% Search for the specified symbol in the data table
symbol = char(jSearchBox.getText);
if ~isempty(symbol)
handles = guidata(hFig);
hTab = handles.hTabGroup.SelectedTab;
colOffset = 0;
forceCol0 = false;
switch hTab.Title
case 'Scanning'
hTable = handles.tbScanResults;
symbols = cell(hTable.Data(:,1));
case 'Correlation'
hTable = handles.tbCorrResults;
symbols = cell(hTable.Data(:,1:2));
case 'Backtesting'
hTab = handles.hBacktestTabGroup.SelectedTab;
hTable = findobj(hTab, 'Type','uitable', 'Tag','results');
pairs = cell(hTable.Data(:,1));
symbols = cellfun(@(c)strsplit(c,'/'), pairs, 'uniform',false);
symbols = reshape([symbols{:}],2,[])';
forceCol0 = true;
case 'Trading'
hTable = handles.tbTrading;
symbols = cell(hTable.Data(:,2:3));
colOffset = 1;
otherwise % ignore
return
end
if isempty(symbols)
return
end
[rows,cols] = ind2sub(size(symbols), find(strcmpi(symbol,symbols)));
if isempty(rows)
% Not found - highlight the search term
jSearchBox.setBackground(java.awt.Color.yellow)
jSearchBox.setForeground(java.awt.Color.red)
jSearchBox.setSelectedTextColor(java.awt.Color.red)
jSearchBox.repaint
elseif isa(eventData, 'java.awt.event.KeyEvent') && isequal(eventData.getKeyCode,10)
% Found with event - highlight the relevant data row
jTable = findjobj(hTable);
try jTable = jTable.getViewport.getView; catch, end % in case findjobj returns the containing scrollpane rather than the jTable
[rows, sortedIdx] = sort(rows);
cols = cols(sortedIdx);
currentRow = jTable.getSelectedRow + 1;
idx = find(rows>currentRow,1);
if isempty(idx), idx = 1; end
if forceCol0
jTable.changeSelection(rows(idx)-1, 0, false, false)
else
jTable.changeSelection(rows(idx)-1, cols(idx)-1+colOffset, false, false)
end
jTable.repaint
jTable.getTableHeader.repaint
jTable.getParent.getParent.repaint
drawnow
end
end
catch
% never mind - ignore
end
end
That’s all there is to it. In my specific case, changing the table’s selected row cased an immediate trigger that updated the associated charts, synchronized the other data tables and did several other background tasks.
The discussion above refers only to traditional Matlab figures (both HG1 and HG2), not to the new web-based (AppDesigner) uifigures that were officially introduced in R2016a (I wrote about it [9] last year).
AppDesigner uifigures are basically webpages rather than desktop windows (JFrames). They use an entirely different UI mechanism, based on HTML webpages served from a localhost webserver, using the DOJO Javascript toolkit [10] for visualization and interaction, rather than Java Swing as in the existing JFrame figures. The existing figures still work without change, and are expected to continue working alongside the new uifigures for the foreseeable future. I’ll discuss the new uifigures in separate future posts (in the meantime you can read a bit about them in my post [9] from last year).
I suspect that the new uifigures will replace the old figures at some point in the future, to enable a fully web-based (online) Matlab [11]. Will this happen in 2017 or 2027 ? – your guess is as good as mine, but my personal guesstimate is around 2018-2020.
Categories: Figure window, GUI, Java, Medium risk of breaking in future versions, Public presentation
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar
URLs in this post:
[1] upcoming presentations: http://undocumentedmatlab.com/blog/upcoming-public-matlab-presentations
[2] auto-completion widgets: http://undocumentedmatlab.com/blog/auto-completion-widget
[3] add custom controls to the figure toolbar: http://undocumentedmatlab.com/blog/figure-toolbar-components
[4] controlling toolbar components’ size: http://undocumentedmatlab.com/blog/figure-toolbar-customizations
[5] findjobj utility: http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object
[6] Matlab’s use of EDT: http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt
[7] here: http://undocumentedmatlab.com/blog/uicontrol-callbacks
[8] here: http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events-in-r2014a
[9] wrote about it: http://undocumentedmatlab.com/blog/sliders-in-matlab-gui#AppDesigner
[10] DOJO Javascript toolkit: https://dojotoolkit.org
[11] web-based (online) Matlab: http://undocumentedmatlab.com/blog/online-web-based-matlab
[12] Figure toolbar components : https://undocumentedmatlab.com/articles/figure-toolbar-components
[13] Figure toolbar customizations : https://undocumentedmatlab.com/articles/figure-toolbar-customizations
[14] Customizing figure toolbar background : https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background
[15] Customizing the standard figure toolbar, menubar : https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar
[16] uicontrol side-effect: removing figure toolbar : https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar
[17] Reverting axes controls in figure toolbar : https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
3 Comments To "Adding a search box to figure toolbar"
#1 Comment By Hanan Kavitz On March 31, 2016 @ 11:02
For great posts like this I keep coming to this blog. Thumbs up!!!
#2 Comment By Wasim Aftab On November 30, 2018 @ 22:25
Undefined function or variable ‘hFig’.
Error in
set(jSearchBox, ‘ActionPerformedCallback’, {@searchSymbol,hFig,jSearchBox})
#3 Comment By Yair Altman On December 1, 2018 @ 17:35
hFig
is (of course) the figure handle. Duh!