% 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
% 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 withevent - 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