扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
private final Color ENABLED_LINE_COLOR = new Color(Display.getCurrent(), 170, 183, 199); private final Color DISABLED_LINE_COLOR = new Color(Display.getCurrent(), 208, 215, 229); private final Color ENABLED_BG = new Color(Display.getCurrent(), 254, 254, 254); private final Color DISABLED_BG = new Color(Display.getCurrent(), 238, 241, 249); private final Image COMBO_ICON = new Image(Display.getDefault(), "combo.png"); |
private Text inputText; private Menu selectorMenu; |
private Object selectedItem; private Vector dataSet = new Vector(); |
public ComboSelector(Composite parent) {...} |
private Point getScreemLocation() { Control control = this; int width = control.getLocation().x; int height = control.getLocation().y; while (control.getParent() != null) { control = control.getParent(); width += control.getLocation().x; height += control.getLocation().y; } return new Point(width, height); } |
super(parent, SWT.FLAT); inputText = new Text(this, SWT.FLAT); selectorMenu = new Menu(this); setMenu(selectorMenu); |
addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { GC gc = e.gc; gc.setForeground(isEnabled() ? ENABLED_LINE_COLOR : DISABLED_LINE_COLOR); gc.drawRectangle(0, 0, getSize().x - 1, getSize().y - 1); gc.drawImage(COMBO_ICON, getSize().x - COMBO_ICON.getBounds().width - 5, (getSize().y - COMBO_ICON.getBounds().height) / 2); } }); |
addControlListener(new ControlAdapter() { @Override public void controlResized(ControlEvent e) { inputText.setBounds(1, 1, getSize().x - COMBO_ICON.getBounds().width - 15, getSize().y - 2); } }); |
public void setEnabled(boolean enabled) { super.setEnabled(enabled); setBackground(enabled ? ENABLED_BG : DISABLED_BG); inputText.setEnabled(enabled); redraw(); } |
public void setEditable(boolean editable) { inputText.setEditable(editable); } public String getText() { return inputText.getText(); } public void setText(String text) { inputText.setText(text); } public void setTextLimit(int limit) { inputText.setTextLimit(limit); } |
inputText.addSelectionListener(new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { commit(); } }); |
protected void commit() {}; |
addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { if (e.x > getBounds().width - COMBO_ICON.getBounds().width - 15 && e.x < getBounds().width && e.y > 0 && e.y < getBounds().height) { selectorMenu.setLocation(getScreemLocation().x + 3, getScreemLocation().y + getSize().y + 23); selectorMenu.setVisible(true); } } }); |
private Vector dataSet = new Vector(); |
public void loadMenuItems(Object[] objects) { dataSet.clear(); MenuItem[] items = selectorMenu.getItems(); for (MenuItem item : items) { item.removeSelectionListener(this); item.dispose(); } for (int i = 0; i < objects.length; i++) { dataSet.add(objects[i]); MenuItem item = new MenuItem(selectorMenu, SWT.PUSH); item.setText(objects[i].toString()); item.setData(objects[i]); item.addSelectionListener(this); } } |
public final void widgetDefaultSelected(SelectionEvent e) public final void widgetSelected(SelectionEvent e) |
public final void widgetSelected(SelectionEvent e) { MenuItem item = (MenuItem) e.getSource(); selectedItem = item.getData(); String text = item.getData().toString(); inputText.setText(text); inputText.setSelection(0, text.length()); selected(item.getData()); } |
protected void selected(Object object) {}; |
public void select(int index) { MenuItem[] items = selectorMenu.getItems(); if (index < 0 || index >= items.length) { throw new ArrayIndexOutOfBoundsException( "the index value must between " + 0 + "and " + (items.length - 1)); } selectedItem = items[index].getData(); inputText.setText(items[index].getText()); } |
package swt.custom; public class Person { private String userName; private String password; public Person(String userName, String password) { this.userName = userName; this.password = password; } public String getPassword() { return password; } public String getUserName() { return userName; } @Override public String toString() { return userName; } } |
final ComboSelector selector = new ComboSelector(this) { @Override protected void commit() { System.out.println("current data is " + ((Person) getSelectedItem()).getUserName()); } @Override protected void selected(Object object) { System.out.println(((Person) object).getPassword()); } }; selector.setBounds(114, 78, 200, 20); Person[] persons = new Person[] { new Person("play_station3@sina.com", "111111"), new Person("rehte@hotmail.com", "222222"), new Person("yitong.liu@bea.com", "password"), new Person("使用其他Windows Live ID 登录", "no") }; selector.loadMenuItems(persons); selector.select(1); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者