Eclipse RCP - how to add mouselistener on the titl

2019-07-23 11:48发布

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条回答
趁早两清
2楼-- · 2019-07-23 12:34

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");
        }

    });

}
查看更多
登录 后发表回答