swt的combo有两个麻烦的问题:
- 如果选中上次选中的内容,它照样会触发addSelectionListener方法
- 如果使用setData(String key, Object value)时,必须设定key值。通常会使用index作为key值。但是如果删除了一项,它的index会自动-1,不能和key相对应了
为了解决这个问题,重新写了一个Combo类
要解决问题1,写了一个新的接口:
- public interface SelectionChangedListener {
-
- public void selectionChanged(SelectionEvent event);
- }
要解决问题2,定义一个类保存combo的状态
- public class ComboState {
- private int selectionIndex = -1;
- public int getSelectionIndex() {
- return selectionIndex;
- }
- public void setSelectionIndex(int selectionIndex) {
- this.selectionIndex = selectionIndex;
- }
- }
Combo类如下:
- public class EasyCombo extends Combo {
- private ComboState state;
- private List<Object> indexData = new LinkedList<Object>();
- private List<SelectionChangedListener> scls = new ArrayList<SelectionChangedListener>();
- public EasyCombo(Composite parent, int style) {
- super(parent, style);
- this.setState(new ComboState());
- }
-
- public void addSelectionChangedListener(SelectionChangedListener listener) {
- if (scls.size() == 0) {
- addSelectionListener(new SelectionListener() {
- public void widgetDefaultSelected(SelectionEvent e) {
- }
- public void widgetSelected(SelectionEvent e) {
- if (getState().getSelectionIndex() != getSelectionIndex()) {
- getState().setSelectionIndex(getSelectionIndex());
- for (SelectionChangedListener listener : scls) {
- listener.selectionChanged(e);
- }
- }
- }
- });
- }
- scls.add(listener);
- }
- public void add(String text) {
- super.add(text);
- indexData.add(null);
- }
- public void add(String text, int index) {
- super.add(text, index);
- if (index > indexData.size()) {
- indexData.add(null);
- } else {
- indexData.set(index, null);
- }
- }
- public void remove(int index) {
- super.remove(index);
- indexData.remove(index);
- }
- public void removeAll() {
- super.removeAll();
- indexData.clear();
- }
-
- public void setIndexData(int index, Object value) {
- indexData.set(index, value);
- }
- public Object getIndexData(int index) {
- return indexData.get(index);
- }
- public void setState(ComboState state) {
- this.state = state;
- }
- public ComboState getState() {
- return this.state;
- }
-
- protected void checkSubclass() {
- }
- }
{zh1}是测试类:
- public class TestEasyCombo {
- public static void main(String[] args) {
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setText("测试窗口");
- shell.setLayout(new FillLayout());
-
- EasyCombo combo = new EasyCombo(shell, SWT.READ_ONLY);
- combo.add("test0");
- combo.setIndexData(0, "测试0");
- combo.add("test1");
- combo.setIndexData(1, "测试1");
- combo.add("test2");
- combo.setIndexData(2, "测试2");
-
- System.out.println("index: "+combo.getIndexData(1));
-
- combo.remove(1);
- System.out.println("index: "+combo.getIndexData(1));
-
-
- combo.addSelectionChangedListener(new SelectionChangedListener(){
- public void selectionChanged(SelectionEvent event) {
- System.out.println("selection changed!");
- }
-
- });
-
- shell.pack();
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- }
- }
好了,新的类完成了。
它提供一个SelectionChangeEvent的事件,当你选中combo的一项,并且不是上次选择的那项时,SelectionChangeEvent才会被触发。此外,我可以根据index获得相相应的值了。
这么做只是解决问题的一种方法。如果不扩展Combo(eclipse建议不要继承swt widget),还有其它的解决方法。下文再写
发表于 @
2008年10月14日 16:27:00 | | | |
|