[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: 2 same mnemonics on the same screen at the same time

The default mnemonic traversal behaviour is to always start looking for a
control with a matching mnemonic at the beginning of the widget order.  You
can override a mnemonic traversal with a Traverse listener, by setting its
event's "doit" to false.  The snippet below shows how to do this with the
simple case of two controls with a common mnemonic (it decides e.doit just
based on isFocusControl()).  To implement this in the general case would
require extra work to decide when e.doit should be set to false, probably
along the lines of "e.doit = does e.widget precede the current focus widget
in the widget order?".

public static void main(String[] args) {
    Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setLayout(new FillLayout());
    for (int i = 0; i < 2; i++) {
        Button btn = new Button(shell, SWT.NONE);
        btn.setText("&Button "+i);
        btn.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e) {
                System.out.println("selected: " +
((Button)e.widget).getText());
            }
        });
        btn.addTraverseListener(new TraverseListener() {
            public void keyTraversed(TraverseEvent e) {
                if (e.detail == SWT.TRAVERSE_MNEMONIC) {
                    if (((Control)e.widget).isFocusControl()) {
                        e.doit = false;
                    }
                }
            }
        });
    }
    shell.setSize (200, 200);
    shell.open ();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

HTH,
Grant


"hao" <d95776@xxxxxxxxx> wrote in message
news:71686ed7eb88b5f5614de08ccef8b239$1@xxxxxxxxxxxxxxxxxx
> In windows xp, for controls except menu/menu items, it does not iterate
> through each of controls. Try the following piece of code:
>
> public static void main (String [] args) {
>     Display display = new Display ();
>     Shell shell = new Shell (display);
>     Menu bar = new Menu (shell, SWT.BAR);
>     shell.setMenuBar (bar);
>     shell.setLayout(new FillLayout());
>     Button btn;
>     for (int i = 0; i < 3; i++) {
>     btn = new Button(shell, SWT.NONE);
>     btn.setText("&Button "+i);
>     if( i!=0 ){
>     btn.addSelectionListener(new SelectionAdapter(){
>
> @Override
> public void widgetSelected(SelectionEvent e) {
> super.widgetSelected(e);
> //MessageDialog.openInformation(e.display.getActiveShell(), "Info",
> ((Button)e.widget).getText());
> System.out.println(((Button)e.widget).getText());
> }
>
>     });
>     }
>         MenuItem fileItem = new MenuItem (bar, SWT.CASCADE);
>         fileItem.setText ("&File");
>         Menu submenu = new Menu (shell, SWT.DROP_DOWN);
>         fileItem.setMenu (submenu);
>         MenuItem item = new MenuItem (submenu, SWT.PUSH);
>         item.addListener (SWT.Selection, new Listener () {
>             public void handleEvent (Event e) {
>                 System.out.println ("Select All");
>             }
>         });
>         item.setText ("Select &All\tCtrl+A");
>         item.setAccelerator (SWT.MOD1 + 'A');
>     }
>     shell.setSize (200, 200);
>     shell.open ();
>     while (!shell.isDisposed()) {
>         if (!display.readAndDispatch ()) display.sleep ();
>     }
>     display.dispose ();
> }
>
>