自动填充有两种
TextContentAdapter 只能用于Text文本框
ComboContentAdapter 只能用于Combo下拉框
AutoCompleteField和ContentProposalAdapter的区别
- AutoCompleteField用于自动完成,例如你输入“山”这个字的时候,可以快速选择输入“山东”或者“山西”。
- ContentProposalAdapter则用于内容建议,你输入“山西”的时候,按下快捷键或者特定字符,会给你“省”、“市”、“自治区”等建议
另外,还可以以给文本框加菜单的形式实现固定输入,在特定监听下,显示某些菜单,然后以此菜单的功能来输入内容。
import org.eclipse.jface.fieldassist.AutoCompleteField;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class AutoDemo {
protected Shell shell;
private Text text;
private Text text_1;
private Menu menu;
private Text text_2;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
AutoDemo window = new AutoDemo();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
@SuppressWarnings("deprecation")
private void createContents() {
shell = new Shell();
shell.setSize(332, 160);
shell.setText("自动完成测试");
shell.setLayout(new GridLayout(1, false));
text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new AutoCompleteField(text, new TextContentAdapter(), new String[] {"山东","山西","河南","河北"});
Combo combo = new Combo(shell, SWT.NONE);
combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new AutoCompleteField(combo, new ComboContentAdapter(), new String[] {"山东","山西","河南","河北"});
text_1 = new Text(shell, SWT.BORDER);
text_1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
Point point = text_1.getParent().toDisplay(text_1.getLocation());
menu.setLocation(point.x, point.y+text_1.getBounds().height);
menu.setVisible(true);
}
});
text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
menu = new Menu(text_1);
text_1.setMenu(menu);
MenuItem mntmNewItem = new MenuItem(menu, SWT.NONE);
mntmNewItem.setText("A");
MenuItem mntmNewItem_1 = new MenuItem(menu, SWT.NONE);
mntmNewItem_1.setText("B");
new MenuItem(menu, SWT.SEPARATOR);
MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
mntmNewSubmenu.setText("C");
Menu menu_1 = new Menu(mntmNewSubmenu);
mntmNewSubmenu.setMenu(menu_1);
MenuItem mntmNewCheckbox = new MenuItem(menu_1, SWT.CHECK);
mntmNewCheckbox.setText("D");
MenuItem mntmE = new MenuItem(menu_1, SWT.NONE);
mntmE.setText("E");
MenuItem mntmF = new MenuItem(menu_1, SWT.NONE);
mntmF.setText("F");
new MenuItem(menu, SWT.SEPARATOR);
MenuItem mntmNewCheckbox_1 = new MenuItem(menu, SWT.CHECK);
mntmNewCheckbox_1.setSelection(true);
mntmNewCheckbox_1.setText("G");
MenuItem mntmNewRadiobutton = new MenuItem(menu, SWT.RADIO);
mntmNewRadiobutton.setSelection(true);
mntmNewRadiobutton.setText("H");
text_2 = new Text(shell, SWT.BORDER);
text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
String[] adviceKey = new String[] {"qq.com","185.com","163.com","126.com","gmail.com","hotmail.com","sina.com","139.com","189.cn"};
ContentProposalAdapter contentProposalAdapter = new ContentProposalAdapter(text_2, new TextContentAdapter(), new SimpleContentProposalProvider(adviceKey), null, new char[] {'@'});
//设置插入方式,忽略、用内容助理中的全部覆盖文本框中的文本、直接在文本框已有文本后面追加等三种方式
contentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT);
//设置是否传播键,即在已经弹出内容窗口的情况下,用户如果继续输入内容,是否允许继续在文本框输入。
contentProposalAdapter.setPropagateKeys(true);
//设置延时,需要弹出内容助理时等待多少毫秒才弹出。
contentProposalAdapter.setAutoActivationDelay(0);
//设置输入内容过滤
//ContentProposalAdapter.FILTER_NONE 永不过滤
// ContentProposalAdapter.FILTER_CHARACTER 只用一个输入字符为条件过滤下面的内容列表.在输入多个字符后,下面的内容列表会被清空.
// ContentProposalAdapter.FILTER_CUMULATIVE 随着用户输入不停的过滤下面的内容列表.但是在3.4后被@deprecated了. 想不通这么实用的东西为什么会被弃用掉
contentProposalAdapter.setFilterStyle(ContentProposalAdapter.FILTER_CUMULATIVE);
//给弹窗设置合适的大小
//contentProposalAdapter.setPopupSize(new Point(10,10));
//如果需要给弹窗中的东西显示图标,则需要设置LabelProvider。
// contentProposalAdapter.setLabelProvider(new LabelProvider());
//给弹窗设置快捷键
// KeyStroke keyStroke = null; // null 表示不接受快捷键
// try {
// keyStroke = KeyStroke.getInstance("Ctrl+1"); // 在text上按Ctrl+1弹出popup的shell.
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}