Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java技巧在Matlab中制作自动完成搜索框_Java_Matlab_Swing - Fatal编程技术网

用Java技巧在Matlab中制作自动完成搜索框

用Java技巧在Matlab中制作自动完成搜索框,java,matlab,swing,Java,Matlab,Swing,我是matlab新手,我想制作一个自动完成搜索框,嵌入到gui中。 我一直在网上寻找,我发现:,这正是我想要做的 但我有两个问题: 1我无法从组合框下拉面板或通过在搜索文本框中键入来选择单词 2如果与列表不匹配,我无法更改单词的背景颜色 我正在使用Matlab2016a 代码如下: function PPP() % function for built the GUI position = [10,100,90,20]; hContainer = gcf; % combobox

我是matlab新手,我想制作一个自动完成搜索框,嵌入到gui中。 我一直在网上寻找,我发现:,这正是我想要做的 但我有两个问题:

1我无法从组合框下拉面板或通过在搜索文本框中键入来选择单词

2如果与列表不匹配,我无法更改单词的背景颜色

我正在使用Matlab2016a

代码如下:

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;
提前谢谢