SWT 在linux下 Combo出现异常

      因为同时在suse和windows xp 下开发Eclipse Plug-in.发现一样的代码,在不同的操作系统中的表现也不一样。最后发现是Combo控件的表现不一致。以下是发现问题的代码:
import  org.eclipse.jface.action.Action;
import  org.eclipse.jface.action.ControlContribution;
import  org.eclipse.jface.action.IAction;
import  org.eclipse.jface.action.ToolBarManager;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.events.ModifyEvent;
import  org.eclipse.swt.events.ModifyListener;
import  org.eclipse.swt.layout.GridData;
import  org.eclipse.swt.layout.GridLayout;
import  org.eclipse.swt.widgets.Combo;
import  org.eclipse.swt.widgets.Composite;
import  org.eclipse.swt.widgets.Control;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Shell;

class  ComboContribution  extends  ControlContribution  {

    
public ComboContribution(String id) {
        
super(id);
    }


    @Override
    
protected Control createControl(Composite parent) {
        
final Combo combo = new Combo(parent, SWT.NONE);
        combo.setItems(
new String[] "Jurassic Park""E.T.""JAW" });
         combo.addModifyListener(
new ModifyListener() {

            
public void modifyText(ModifyEvent e) {
                String text 
= combo.getText();
                System.out.println(
"========== " + text);
            }

        }
);
//        combo.addSelectionListener(new SelectionAdapter() {
//            public void widgetSelected(SelectionEvent e) {
//                String text = combo.getText();
//                System.out.println("==========  " + text);
//            }
//        });
        return combo;
    }

}


public   class  TestToolBar  {

    
public TestToolBar(Shell shell) {
        IAction runAction 
= new Action("Run"{

            
public void run() {

            }

        }
;

        ToolBarManager barManager 
= new ToolBarManager(SWT.NONE);
        barManager.add(runAction);

        ComboContribution combo 
= new ComboContribution("Combo.contribution");
        barManager.add(combo);

        barManager.createControl(shell);

        GridData gd 
= new GridData(GridData.FILL_HORIZONTAL);
        barManager.getControl().setLayoutData(gd);
    }


    
/**
     * DOC qianbing Comment method "main".
     * 
     * 
@param args
     
*/

    
public static void main(String[] args) {
        Display display 
= new Display();

        
final Shell shell = new Shell(display);
        shell.setLayout(
new GridLayout());

        
new TestToolBar(shell);

        shell.setSize(
300300);
        shell.open();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }

}

         Combo加入 ModifyListener后,在Windows下是使用正常,但是在Suse下,监听器被调用了2次。而且第一次调用取得的Combo.getText()是“”,第二次才正常。严重影响了程序的流程。后来决定使用SelectionListener, 在 在Windows下每次选择都会调用监听器,但是在Suse下,只有选择不同的选项,才会调用监听器。因为SWT是调用操作系统本地控件,所以各个操作系统的表现很可能有出入,其可移植性还是有待提高。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值