Search code examples
javaeclipseswteclipse-rcpjface

Why Selection event not triggered when selection is done using keyboard?


I have this Eclipse RCP application which uses SWT. Here is a sample code.

    Combo combo = new Combo(shell, SWT.NONE);
    combo.setItems(items); // items is a String[]
    combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

    combo.addSelectionListener(new SelectionAdapter() {
         
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            System.out.println("In widgetDefaultSelected");
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.println("In widgetSelected");
        }
    }); 

The combo has been set up in the code for auto complete. The selection event is supposed to get triggered for mouse or keyboard events. A selection using mouse triggers the selection event but one with keyboard does not. I am trying to see why.

My eclipse is not the latest, it is version is 3.6.2 and the swt JARs that come with it. I would appreciate any help.


Solution

  • Since the selection event is not triggered with keyboard, I added a KeyListener to the combo widget and check to see if the user has pressed enter key.

        combo.addKeyListener(new KeyListener() {
    
            @Override
            public void keyPressed(KeyEvent e) {
    
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.keyCode==SWT.CR || e.keyCode==SWT.KEYPAD_CR) { // Enter key
                    Combo c = (Combo) e.getSource();
                    System.out.println(c.getText());
                    // Do rest of processing
                }
            }
        });
    

    Seems like I am getting the selected item out of the list box. So far it seems to be working OK.