组合框rcp中自动完成

时间:2016-06-21 10:40:03

标签: combobox autocomplete rcp

我想在组合框中提供自动完成功能。

final String [] ComboDataArr = ComboData.toArray(new String[ComboData.size()]);
        editor = new TableEditor(Table);
        Combo combo = new Combo(Table, SWT.DROP_DOWN);
        combo.setText("Choose the signal");
        Arrays.sort(ComboDataArr);
        for(int j= 0;j < ComboDataArr.length;j++){
            combo.add(ComboDataArr[j]);
        }
        ComboContentAdapter comboAdapter = new ComboContentAdapter(); 
        IContentProposalProvider proposalProvider = new IContentProposalProvider() {

            @Override
            public IContentProposal[] getProposals(String contents, int position) {
                List<IContentProposal> validProposals = new ArrayList<IContentProposal>();
                for (String comboAutoProposals : ComboDataArr) {
                    contents = contents.substring(0, position);
                    for (byte b : contents.getBytes()) {
                        char c = (char) (b & 0xFF);
                        if (comboAutoProposals.indexOf(c) != -1) { // This is where it checks if the proposal contains the chars.
                            validProposals.add(new ContentProposal(comboAutoProposals));
                            break;
                        }
                    }
                }
                return validProposals.toArray(new IContentProposal[validProposals.size()]);
            }
        };
        ContentProposalAdapter adapter = new ContentProposalAdapter(combo, comboAdapter, proposalProvider, null, null);
        adapter.setPropagateKeys(true);
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
        editor.grabHorizontal = true;
        editor.setEditor(combo, Items[i], 1);

但它显示了包含我输入的角色的所有可能性。如果我输入BH,它会显示包含B,BH和H的所有条目。我希望它只显示以我输入的字符开头的条目,如果我输入BH,它应该显示以BH开头的条目。

2 个答案:

答案 0 :(得分:0)

尝试通过设置

来更改提案接受方式
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT);

答案 1 :(得分:0)

用于rcp中的ComboBox的自动完成功能

private static final String LCL = "abcdefghijklmnopqrstuvwxyz";
private static final String UCL = LCL.toUpperCase();
private static final String NUMS = "0123456789";

 static  void enableContentProposal(Control control)
{

    SimpleContentProposalProvider proposalProvider = null;
    ContentProposalAdapter proposalAdapter = null;
    if (control instanceof Combo)
    {
        Combo combo = (Combo) control;
        proposalProvider = new SimpleContentProposalProvider(combo.getItems());
        proposalAdapter = new ContentProposalAdapter(
                combo,
                new ComboContentAdapter(),
                proposalProvider,
                getActivationKeystroke(),
                getAutoactivationChars());
    }
    proposalProvider.setFiltering(true);
    proposalAdapter.setPropagateKeys(true);
    proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

}


static char[] getAutoactivationChars() {

    String delete = new String(new char[] { 8 });
    String allChars = LCL + UCL + NUMS + delete;
    return allChars.toCharArray();
}

static KeyStroke getActivationKeystroke() {
    KeyStroke instance = KeyStroke.getInstance(
            new Integer(SWT.CTRL).intValue(), new Integer(' ').intValue());
    return instance;
}

致电

enableContentProposal(tableComboBox) 
创建ComboBox后