使用Java技巧在Matlab中创建一个自动完成搜索框

时间:2016-12-01 12:24:52

标签: java matlab swing

我是matlab的新手,我想制作一个自动填充搜索框来嵌入gui。 我一直在寻找互联网,我发现了这个:http://undocumentedmatlab.com/blog/auto-completion-widget,这正是我想做的 但我有两个问题:

1)我无法从组合框下拉列表中选择单词或在搜索文本框中输入

2)如果与列表

不匹配,我无法更改单词的背景颜色

我正在使用matlab 2016a

这是代码:

function PPP()

%  function for built the GUI

position = [10,100,90,20];  
hContainer = gcf;  

%  combobox

options = {'a','b','c','cippo','pippo','lure','azurra','che','cosa','stai','scriviunacosalunghissima'};
model = javax.swing.DefaultComboBoxModel(options);
jCombo = javacomponent('javax.swing.JComboBox', position, hContainer);
jCombo.setModel(model);
jCombo.setEditable(true);


% Create the SearchTextField (after the hidden combo was created)
jAssetChooser = com.mathworks.widgets.SearchTextField('Enter search:');
jAssetComponent = jAssetChooser.getComponent;
javacomponent(jAssetComponent,position,hContainer);

%  Set callbacks
hjSearchButton = handle(jAssetComponent.getComponent(1), 'CallbackProperties');
set(hjSearchButton, 'MouseClickedCallback', {@updateSearch,jCombo,jAssetChooser});

hjSearchField = handle(jAssetComponent.getComponent(0), 'CallbackProperties');
set(hjSearchField, 'KeyPressedCallback', {@updateSearch,jCombo,jAssetChooser});

jComboField = handle(jCombo.getComponent(2), 'CallbackProperties');
set(jComboField, 'KeyPressedCallback', {@updateSearch,jCombo,[]});

In this way i should be able to select an item either from the combo-boxs dropdown panel, or by typing in the search text-box.


% Asset search popup combo button click callback
function updateSearch(hObject, eventData, jCombo, jAssetChooser) %#ok<INUSL>
    persistent lastSearchText
    if isempty(lastSearchText),  lastSearchText = '';  end

    try
        % event occurred on the search field component
        try
            searchText = jAssetChooser.getSearchText;
            jSearchTextField = jAssetChooser.getComponent.getComponent(0);
        catch
            % Came via asset change - always update
            jSearchTextField = jAssetChooser.getComponent(0);
            searchText = jSearchTextField.getText;
            lastSearchText = '!@#$';
        end
    catch
        try
            % event occurred on the jCombo-box itsel
            searchText = jCombo.getSelectedItem;
        catch
            % event occurred on the internal edit-field sub-component
            searchText = jCombo.getText;
            jCombo = jCombo.getParent;
        end
        jSearchTextField = jCombo.getComponent(jCombo.getComponentCount-1);
    end
    searchText = strrep(char(searchText), '*', '.*');  % turn into a valid regexp
    searchText = regexprep(searchText, '<[^>]+>', ''); 
    if strcmpi(searchText, lastSearchText) && ~isempty(searchText)
        jCombo.showPopup;
        return;  
    end
    lastSearchText = searchText;

% find the word into the list  

searchComponents = strsplit(searchText, '-');
AssetNameIdx =~cellfun('isempty',regexpi(options,searchComponents{end}));

AssetNamesIdx = AssetNameIdx;   

% in this part change the colour if the word doesn't match 

    if isempty(AssetNamesIdx) 
      jCombo.hidePopup;       
      jSearchTextField.setBackground(java.awt.Color.yellow);
        jSearchTextField.setForeground(java.awt.Color.red);
        newFont = jSearchTextField.getFont.deriveFont(uint8(java.awt.Font.BOLD));
        jSearchTextField.setFont(newFont);
        return;
    else
        jSearchTextField.setBackground(java.awt.Color.white);
        jSearchTextField.setForeground(java.awt.Color.black);
        newFont = jSearchTextField.getFont.deriveFont(uint8(java.awt.Font.PLAIN));
        jSearchTextField.setFont(newFont);
    end
   % Compute the filtered asset
    assetNames = strcat(options(AssetNamesIdx),' ');
    assetNames = regexprep(assetNames, '(.+) -=\1', '$1', 'ignorecase');
    assetNames = unique(strrep(assetNames, ' -=', ' - '));
    if ~isempty(searchText)
        assetNames = regexprep(assetNames, ['(' searchText ')'], '<b><font color=blue>$1</font></b>', 'ignorecase');
        assetNames = strcat('<html>', assetNames);

    end
  % % Redisplay the updated combo-box popup panel
  % jCombo.setModel(javax.swing.DefaultComboBoxModel(assetNames));
  % jCombo.showPopup;
end;
end;

提前致谢。

0 个答案:

没有答案