0

I have a strange problem with MessageDialog. I have a dialog which holds a TableViewer. My plan is that when a user select something in the TableViewer, it will pop up a warning dialog. the code is like below:

contextTableViewer = new TableViewer(tableContainer, SWT.BORDER | SWT.FULL_SELECTION);
contextTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  public void selectionChanged(SelectionChangedEvent event) {
    ...
    if () {
       Shell pshell = Display.getCurrent().getActiveShell();
        MessageDialog.openWarning(pshell, "something", "something");
    }
    ...
  }

my problem is that, after the warning dialog pop up, I loss the focus. For instance, I need to click "Cancel" button twice to close the dialog that holds the TableViewer. I tried to use setFocus() to set the focus back, but it didn't work. Any tips? Thanks very much.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
chygo
  • 366
  • 2
  • 6
  • 16
  • It probably makes no different here but you can just call `getShell()` in a dialog, no need for `Display.getCurrent().getActiveShell()` – greg-449 Aug 29 '14 at 15:46
  • Do you mean you loose focus while the MessageDialog is displayed or after it is closed? – greg-449 Aug 29 '14 at 15:47
  • Thanks, I just tried ur method, `getShell()` didn't work as well. I also tried `getShell().setActive()`, `getShell.setFocus()`, after the openWarning, they didn't work~ – chygo Aug 29 '14 at 15:50
  • After. After I close the MessageDialog, I need to click the "Cancel" button twice to close the dialog that holds the table. – chygo Aug 29 '14 at 15:52
  • I can't reproduce this. Check the .log file in the workspace .metadata directory for any related messages. – greg-449 Aug 29 '14 at 16:10
  • I just recall that it happened before, also in the TableViewer, one of the column in the table allow users to edit the values unless it is 0 (for example). If it is 0, it will pop up a MessageDialog. After I close the MessageDialog, I need to click the cell, which I was editing, twice to get the focus back. – chygo Aug 29 '14 at 16:18
  • BTW, when I debug my program, I set a breakpoint in the method `cancelPressed()`. The first time I click the "Cancel" button, the debugger dosen't jump to the breakpoint, until I click the button for the second time. – chygo Aug 29 '14 at 16:26

1 Answers1

0

I don't know why it happens, I solve the problem by mocking a mouse clicking on the dialog to get the focus back.

chygo
  • 366
  • 2
  • 6
  • 16