Standard Widget Toolkit
-- Création d'une application SWT
Installer au besoin Window Builder Pro dans Eclipse.
- Création d'un projet : Choisir File/New/other/Window Builder/SWT/JFace java Project
- Choisir File/New/other/SWT/Application Window
-- Structure d'une application
public class MainWindow {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
MainWindow window = new MainWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
}
}
-- Mode Design
-- Implémentation du comportement
Nous allons ajouter les comportements suivants :
- Sur la sélection d'une des options, le texte de l'option sélectionnée apparaît dans la zone de texte. Le activeButton devient l'option sélectionnée
- Sur le click du bouton Valider, le texte entré dans la zone de texte remplace le texte de l'option sélectionnée (activeButton)
-- Ajout de listener
- Sélectionner L'option 1, faire apparaître le menu contextuel avec le bouton droit de la souris :
- Choisir add Event Handler/ Selection/ WidgetSelected
Le listener est implémenté via une classe anonyme :
btnOption.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
- Ajouter un membre privé Button activeButton
- Implémenter le code suivant :
@Override
public void widgetSelected(SelectionEvent e) {
activeButton = (Button) e.getSource();
text.setText(activeButton.getText());
}
-- Ajout d'un même listener sur les évènements de plusieurs Widgets
SelectionAdapter sa = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
activeButton = (Button) e.getSource();
text.setText(activeButton.getText());
}
};
btnOption.addSelectionListener(sa);
btnOption_1.addSelectionListener(sa);
--Divers
-- Chargement d'une image
public static Image loadImage(String path, boolean inJar) {
Image newImage = null;
try {
if (inJar)
{
newImage = new Image(null, App.class.getClassLoader().getResourceAsStream(path));
}
else {
newImage = new Image(null, path);
}
} catch (SWTException ex) {
ex.printStackTrace();
}
return newImage;
}


