The default button is the button that gains the focus and is pressed automatically when a SWT.TRAVERSE_RETURN type traversal happens.
public Button getDefaultButton()
public void setDefaultButton(Button defaultButton)
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonDefaultShell {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
final String[] ratings = new String[] { "A!", "B", "C" };
final Button[] radios = new Button[ratings.length];
for (int i = 0; i < ratings.length; i++) {
radios[i] = new Button(shell, SWT.RADIO);
radios[i].setText(ratings[i]);
}
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Canel");
Button rateButton = new Button(shell, SWT.PUSH);
rateButton.setText("OK");
rateButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < radios.length; i++) {
if (radios[i].getSelection()) {
System.out.println(ratings[i]);
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
for (int i = 0; i < radios.length; i++) {
if (radios[i].getSelection()) {
System.out.println(ratings[i]);
}
}
}
});
shell.setDefaultButton(rateButton);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}