组合框 rcp 中的自动完成
AutoComplete in combo box 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 开头的条目。
尝试通过设置
更改提案接受样式
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT);
对于 rcp 中组合框的自动完成功能
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)
组合框创建后。
我想在组合框中提供自动完成功能。
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 开头的条目。
尝试通过设置
更改提案接受样式adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT);
对于 rcp 中组合框的自动完成功能
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)
组合框创建后。