Java SWT: mouseUp() is not fired when opening a Dialog in mousDown()

Sadik :

My program opens a dialog if a certain string is clicked inside a StyledText. So in the mouseDown() I first want to check what has been clicked and then open a dialog. This works. After closing the dialog the mouseUp() is not called. This leads to selecting the text when moving the cursor, as if the user tries to select a text. I can reproduce the behavior by performing the following tasks:

  • Click on String in StyledText -> Dialog Opens
  • Close Dialog
  • Move Mouse without clicking -> Text gets marked as selected

In my use case I don't need mouseUp() to be fired. But having it not fired means the OS assumes that the mouse button is still down and selects text. This may be the correct behavior if a dialog opens and steals the focus. But than there must be a possibility to tell the system, that the mouse button has been released.

myStlyedText.addMouseListener(new MouseListener() {
    @Override
    public void mouseUp(MouseEvent e) {
        System.out.println("MouseUp is fired");
    }

    @Override
    public void mouseDown(MouseEvent e) {
        if (certainStringClicked()) {
            openDialog();
        }
    }

    @Override
    public void mouseDoubleClick(MouseEvent e) {}
});

I can verify that mouseUp() is not called because "MousUp is fired" is not printed on console.

What is the best way to handle this? I already tried to set focus on another widget (setFocus() and forceFocus()), but that didn't help.

I tried to call mouseUp myself:

Event event = new Event();
event.type = SWT.MouseUp;
event.button = 1;
MouseEvent mouseUpEvent = new MouseEvent(event);
mouseUp(mouseUpEvent);

This leads to the message "MousUp is fired", but the selection problem still exists.

I could move the code into the mouseUp() method, but that's not actually what I want. The dialog should appear immediately. What else can I do?

Yaroslav Nudnenko :

Try adding myStlyedText.notifyListeners(SWT.MouseUp, null); to your code. It should work.

myStlyedText.addMouseListener(new MouseListener() {
    @Override
    public void mouseUp(MouseEvent e) {
        System.out.println("MouseUp is fired");
    }

    @Override
    public void mouseDown(MouseEvent e) {
        if (certainStringClicked()) {
            myStlyedText.notifyListeners( SWT.MouseUp, null );
            openDialog();
        }
    }

    @Override
    public void mouseDoubleClick(MouseEvent e) {}
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145860&siteId=1