1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MySecondDialog extends Dialog {
public MySecondDialog(Shell parent) {
super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
public String open(){
Shell shell = new Shell(Display.getCurrent(), getStyle());
shell.setText("My second dialog");
shell.setLayout(new GridLayout(2, false));
addContents(shell);
shell.pack();
shell.open();
Display display = Display.getCurrent();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Using my second dialog");
shell.setLayout(new RowLayout(SWT.VERTICAL));
final Text value = new Text(shell, SWT.READ_ONLY);
Button button = new Button(shell, SWT.PUSH);
button.setText("Changer la valeur");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
MySecondDialog dialog = new MySecondDialog(shell);
String result = dialog.open();
if (result.length() > 0)
value.setText(result);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void addContents(final Shell shell) {
new Label(shell, SWT.NONE).setText("Statut");
final Combo combo = new Combo(shell, SWT.DROP_DOWN);
for (String currentValue : values)
combo.add(currentValue);
Button okButton = new Button(shell, SWT.PUSH);
okButton.setText("Ok");
okButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
result = combo.getText();
shell.close();
}
});
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
result = "";
shell.close();
}
});
}
private String result;
private final String values [] = {"Monsieur", "Madamme", "Mademoiselle"};
} |
Partager