Installer au besoin Window Builder Pro dans Eclipse.
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"); } }
Nous allons ajouter les comportements suivants :
Le listener est implémenté via une classe anonyme :
btnOption.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { } });
@Override public void widgetSelected(SelectionEvent e) { activeButton = (Button) e.getSource(); text.setText(activeButton.getText()); }
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);
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; }