Globally disable enter / return key in SWT dialog

I created a dialog class that populates some common widgets, such as text, combos, and a tree. This is annoying when the default behavior rejects this dialog (the same as pressing the “OK” button by default) when the enter / return button is pressed, whatever widget I am.

To prevent this behavior, I have to add a traverse listener for each widget to filter out the move symbol:

if (SWT::TRAVERSE_RETURN == event.detail) {
  event.doit = false 
}

This is somewhat annoying. Is there a way to do it globally at the dialogue level?

+3
source share
5 answers

I found that in the JFace dialog it is easy to disable the Enter key by default by overriding the method createButtonsForButtonBar.

:

createButton (, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, );

:

@Override
protected void createButtonsForButtonBar(Composite parent) {
Button button = createButton(parent, IDialogConstants.OK_ID,
        IDialogConstants.OK_LABEL, false);
}
+3

, , -, , Snippet127 ( " Tab " )

, Dialog class TraverseListener keyTraversed().

0

, null (Shell # setDefaultButton (null)), , , , , Enter.

0

, , , , . , , , createButtonsForButtonBar().

    final Button hiddenButton = new Button(parent, SWT.PUSH);
    hiddenButton.setVisible(false);
    hiddenButton.setEnabled(false);
    parent.getShell().setDefaultButton(hiddenButton);

. , , .

0

, , , , 2009 ... .

Display.addFilter. - .

Display.getDefault().addFilter( SWT.Traverse, new Listener() {
    @Override
    public void handleEvent( Event event ) {
        if( SWT.TRAVERSE_RETURN == event.detail ) {
            System.out.println( "Global SWT.TRAVERSE_RETURN" );
            event.doit = false;
        }
    };
} );

, :

Shell shell = new Shell( SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL );
shell.setLayout( new GridLayout( 2, false ) );
shell.setSize( 250, 100 );

Label loginLabel = new Label( shell, SWT.NONE );
loginLabel.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, false, false ) );
loginLabel.setText( "Login" );

Text text = new Text( shell, SWT.BORDER );
text.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );

Composite buttonsContentComposite = new Composite( shell, SWT.NONE );
buttonsContentComposite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 2, 1 ) );
buttonsContentComposite.setLayout( new GridLayout( 2, false ) );

Button okButton = new Button( buttonsContentComposite, SWT.PUSH );
okButton.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, false ) );
okButton.setText( "Ok" );
okButton.addSelectionListener( new SelectionAdapter() {
    @Override
    public void widgetSelected( SelectionEvent e ) {
        System.out.println( "Ok" );
    };
} );

Button cancelButton = new Button( buttonsContentComposite, SWT.PUSH );
cancelButton.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, false, false ) );
cancelButton.setText( "Cancel" );
cancelButton.addSelectionListener( new SelectionAdapter() {
    @Override
    public void widgetSelected( SelectionEvent e ) {
        System.out.println( "Cancel" );
    };
} );

shell.setDefaultButton( okButton );

shell.open();
while (!shell.isDisposed()) {
    if( !Display.getCurrent().readAndDispatch() ) {
        Display.getCurrent().sleep();
    }
}

The filter captures the intersection event and prevents it from being sent to the shell, so the button will not work by default. But this solution has one nuance - you can add a Traverse listener for the shell and override the effect of the global filter:

shell.addTraverseListener( new TraverseListener() {
    @Override
    public void keyTraversed( TraverseEvent event ) {
        event.doit = true;
    }
} );

This button to enable the return event is triggered by default.

0
source

Source: https://habr.com/ru/post/1711311/


All Articles