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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
// Fait la fenêtre
Shell shell = new Shell(display);
shell.setBounds(10, 10, 300, 300);
shell.setLayout(new GridLayout(2, false));
// Créé un bouton
Button bouton = new Button(shell, SWT.PUSH);
bouton.setText("Fait des trucs");
bouton.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
// Créé un autre bouton
Button bouton2 = new Button(shell, SWT.PUSH);
bouton2.setText("Fait d'autres trucs");
bouton2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
// Comp est déclaré en final car sinon on ne peut pas les utiliser dans les classes anonymes
// On aurait pu le déclarer en champ de la classe
final Composite comp = new Composite(shell, SWT.BORDER);
comp.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
comp.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
comp.setLayout(new GridLayout(2, false));
// Actions quand on clique sur un bouton
bouton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
faitDesTrucs(comp);
}
});
bouton2.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
faitDAutresTrucs(comp);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* @param comp
*/
protected static void faitDAutresTrucs(Composite comp) {
// D'abord on nettoie le composite de tous les éléments déjà présents
nettoie(comp);
// Puis on ajoute des widgets
final Label designation = new Label(comp, SWT.NONE);
designation.setText("Désignation :");
final Text designationText = new Text(comp, SWT.BORDER);
final Label numSerie = new Label(comp, SWT.NONE);
numSerie.setText("Numéro de série :");
final Text numSerieText = new Text(comp, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.widthHint = 300;
gd.heightHint = 25;
numSerieText.setLayoutData(gd);
final Label quantite = new Label(comp, SWT.NONE);
quantite.setText("Quantité :");
final Text quantiteText = new Text(comp, SWT.BORDER);
quantiteText.setSize(300, 25);
// Maintenant grâce à layout, on redessine le composite
comp.layout();
}
/**
* @param comp
*/
protected static void faitDesTrucs(Composite comp) {
nettoie(comp);
Group grp = new Group(comp, SWT.NONE);
grp.setText("Coucou");
grp.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
grp.setLayout(new FillLayout());
Button bouton = new Button(grp, SWT.PUSH);
bouton.setText("Hop là");
comp.layout();
}
/**
* @param comp
*/
private static void nettoie(Composite comp) {
if (comp == null || comp.isDisposed()) {
return;
}
for (Control current : comp.getChildren()) {
if (!current.isDisposed()) {
current.dispose();
}
}
}
} |