您的位置:首页 > 运维架构 > Linux

SWT 在linux下 Combo出现异常

2008-04-10 06:25 295 查看
      因为同时在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(300, 300);


        shell.open();




        while (!shell.isDisposed()) ...{


            if (!display.readAndDispatch())


                display.sleep();


        }


        display.dispose();


    }


}


         Combo加入ModifyListener后,在Windows下是使用正常,但是在Suse下,监听器被调用了2次。而且第一次调用取得的Combo.getText()是“”,第二次才正常。严重影响了程序的流程。后来决定使用SelectionListener, 在在Windows下每次选择都会调用监听器,但是在Suse下,只有选择不同的选项,才会调用监听器。因为SWT是调用操作系统本地控件,所以各个操作系统的表现很可能有出入,其可移植性还是有待提高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: