制作简单的搜索引擎
利用swt的Combo下拉菜单控件制作一个简单的类似百度的搜索引擎,假如当在combo的文本框中输入a时,能够读取到数据库中数据a,ab,abc,abcd然后填充到combo的下拉菜单中;当输入ab时,下拉菜单为ab,abc,abcd;当输入abc时,下拉菜单为abc,abcd。就是在文本框文本值更改事件方法中写入:
Searchcombo.addListener(SWT.Modify, new Listener() { @Override public void handleEvent(Event arg0) { String value=Searchcombo.getText().trim().toLowerCase(); if (Searchcombo.getItemCount()!=0) { Searchcombo.removeAll(); } //去读数据库 List<String> keys=getsss(); for (String key : keys) { if (key.indexOf(value)!=-1) { Searchcombo.add(key); System.out.println(Searchcombo.getItemCount()); } } } });
Searchcombo.addListener(SWT.Modify, new Listener() { @Override public void handleEvent(Event arg0) { String value=Searchcombo.getText().trim().toLowerCase(); if (Searchcombo.getItemCount()!=0) { String[] removeItem=Searchcombo.getItems(); //循环删除下拉菜单选项 for (int i = 0; i < removeItem.length; i++) { System.out.println(removeItem[i]+"--"); Searchcombo.remove(removeItem[i]); } Searchcombo.setText(value); } List<String> keys=getsss(); for (String key : keys) { if (key.indexOf(value)!=-1) { Searchcombo.add(key); System.out.println(Searchcombo.getItemCount()); } } } });
public void handleEvent(Event arg0) { String value = searchCombo.getText().trim().toLowerCase(); String[] items = searchCombo.getItems(); int selection = searchCombo.getSelectionIndex(); String selectText; if (selection==-1) selectText = null; else selectText = searchCombo.getItem(searchCombo.getSelectionIndex()); for (int i=0; i<items.length; i++) { if (selection!=i) searchCombo.remove(items[i]); } // 去读数据库 List<String> keys = getsss(); for (String key : keys) { if (key.indexOf(value) != -1 && !key.equals(selectText)) { searchCombo.add(key); System.out.println(searchCombo.getItemCount()); } }