user interface - Java SWT menubar item stuck "on" after subshell created -


this undoubtedly trivial problem, cannot seem figure out i'm doing wrong. situation simple: have application creates additional dialog window show user. main application has menubar menu items have keyboard shortcuts. when invoke menu item using keyboard shortcut, , main program creates new window, when user closes new window , shown main application again, menubar item stays stuck highlighted/"turned on" look. code replicate i'm seeing on mac os swt 4.2 this:

import org.eclipse.swt.swt; import org.eclipse.swt.events.selectionadapter; import org.eclipse.swt.events.selectionevent; import org.eclipse.swt.layout.filllayout; import org.eclipse.swt.widgets.button; import org.eclipse.swt.widgets.display; import org.eclipse.swt.widgets.menu; import org.eclipse.swt.widgets.menuitem; import org.eclipse.swt.widgets.shell;  public class tester {     public static void createshell(shell parent) {         final shell newshell = new shell(parent, swt.dialog_trim | swt.resize);         newshell.setsize(100,100);         newshell.setlayout(new filllayout());          button closebutton = new button(newshell, swt.none);         closebutton.settext("close");         closebutton.addselectionlistener(new selectionadapter() {             @override             public void widgetselected(selectionevent arg0) {                 newshell.close();             }         });          newshell.open();     }      public static void main(string[] args) {         display display = new display();         final shell shell = new shell(display);         shell.setlayout(new filllayout());         shell.setsize(200, 100);          menu menubar = new menu(shell, swt.bar);         shell.setmenubar(menubar);          menuitem item = new menuitem(menubar, swt.cascade);         item.settext("foo");          menu foomenu = new menu(item);         item.setmenu(foomenu);          menuitem barmenu = new menuitem(foomenu, swt.none);         barmenu.settext("menu item");         barmenu.setaccelerator(swt.mod1 + 'f');         barmenu.addselectionlistener(new selectionadapter() {             @override             public void widgetselected(selectionevent arg0) {                 createshell(shell);             }         });          shell.open();         while (!shell.isdisposed()) {             if (!display.readanddispatch()) {                 display.sleep();             }         }         display.dispose();     } } 

to replicate problem, run program above, invoke command-f keyboard, click on "close" button in window created. close 2nd window , go original window. here's example of looks after that:

screenshot

the problem how "foo" stays highlighted. expect not stay highlighted. , in fact, not stay highlighted if pull down menu , select menu item, there specific using keyboard shortcut causes this, i'm @ wits' end trying figure out be.

can please tell me i'm doing wrong?

i can't see being wrong in code. left work around issue. while indicates hack, work. idea defer execution later time, not later, after sufficient time, event complete , item being deactivated. first test of using display.getdefault().asyncexec() did not work expected, instead i've tried combination of invokelater asyncexec not anything. @ point choose hammer , called thread.currentthread().sleep() in combination of above, , seems work fine.

all need change selectionadapter of barmenu so:

    barmenu.addselectionlistener(new selectionadapter() {         @override         public void widgetselected(selectionevent arg0) {             // invoke later give event time finish, , have             // menu item deselected             javax.swing.swingutilities.invokelater(new runnable() {               public void run() {               // wait               try { thread.currentthread().sleep(100);} catch(exception ex){}               // jump swt thread , actual work               display.getdefault().asyncexec(new runnable() {               public void run() {                createshell(shell);               }});             }});         }     }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -