Eclipse RCP - how to add mouselistener on the titlebar of an eclipse ScrolledForm

筅森魡賤 提交于 2019-12-11 04:37:00

问题


I try to use the addmouselistener method of the form and check if the clicks are in the area of the title bar, but the mouse listener is not working. I tried to add the mouse listener on form and on form.getform() and on form.getBody() neither one works.

Here is the code where I am creating the form and trying to add a mouse listener on it:

        toolKit = new FormToolkit(parent.getDisplay());
    form = toolKit.createScrolledForm(parent);

    FillLayout layout = new FillLayout();
    layout.type = SWT.VERTICAL;
    layout.marginHeight = 10;
    layout.marginWidth = 4;

    canvas = new FigureCanvas(form.getBody(), SWT.DOUBLE_BUFFERED );
    canvas.setViewport(new FreeformViewport());
    canvas.setBackground(ColorConstants.white);
    canvas.setContents(root);
    form.getBody().setLayout(layout);
    form.setText("Data Transactions View");

    createHeaderRegion(form);


    toolKit.decorateFormHeading(form.getForm());
    form.getToolBarManager().add(new Action("This") { });

    form.getForm().addMouseListener(new MouseListener(){

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseDown(MouseEvent e) {

            if(e.x<10 && e.y<10){
                form.getToolBarManager().add(new Action("This is the toolbar") { });    // NEW LINE
                form.getToolBarManager().update(true);  // NEW LINE
            }
        }

        @Override
        public void mouseUp(MouseEvent e) {
            // TODO Auto-generated method stub

            }
        }});

Any suggestions?


回答1:


You have to add the Listener to the head of the Form.

public void createPartControl(Composite parent) {

    ScrolledForm scrolledForm = formToolkit
            .createScrolledForm(parent);
    formToolkit.paintBordersFor(scrolledForm);
    scrolledForm.setText("New ScrolledForm");
    formToolkit.decorateFormHeading(scrolledForm.getForm());

    scrolledForm.getForm().getHead().addMouseListener(new MouseListener() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            System.out.println("mouseDoubleClick");
        }

        @Override
        public void mouseDown(MouseEvent e) {
            System.out.println("mouseDown");
        }

        @Override
        public void mouseUp(MouseEvent e) {
            System.out.println("mouseUp");
        }

    });

}


来源:https://stackoverflow.com/questions/11344894/eclipse-rcp-how-to-add-mouselistener-on-the-titlebar-of-an-eclipse-scrolledfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!