, , , , 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.
source
share