This document provides steps to create a simple Java SWT application in Eclipse:
1) Install Java JDK and Eclipse RCP IDE, create a Java project, add the SWT jar, and generate a class with a click counter;
2) Implement the click counter logic and run the application;
3) Export the project as a runnable JAR file that can be deployed and run on other machines with Java installed.
2. SWT Applications in minutes In this short presentation we will learn how to develop and deploy a simple SWT Application in a matter of minutes.
3. Requirements The setup process however is not included in these minutes, because you will have to do some download and setting process. Anyway this is a one-time task!
5. Download Eclipse RCP distro Go to the Eclipse download page: http://www.eclipse.org/downloads and download the RCP version of Eclipse
6. Extracting Eclipse RCP IDE Extract the downloaded file (e.g. eclipse-rcp-ganymede-SR1-win32.zip) somewhere on your local drive (e.g. on C:) Note: do not use the the Compressed Folder unzipper shipped with the operating system since it may not extract hidden files
7. Running Eclipse RCP IDE Run the Eclipse executable file (e.g. C:clipseclipse.exe)
11. Create a Java Project Enter a name for the Project (e.g. FirstSwtProject) and press the Next button
12. Create a Java Project Select the Libraries tab and press the Add External JARs ... button
13. Create a Java Project Browse and select the file <eclipse_dir>/plugins/ org.eclipse.swt.win32.win32.x86_3.4.1.v3449c.jar
14. Create a Java Project Select the Order and Export tab and check the jar file we just added (this will export this jar file in the deploy phase) and press the Finish button
15. Create a Java Class Expand the Project tree, select the src folder and right-click New, Class
16. Create a Java Class Enter a package name for the new class (e.g. com.yourcompany.swt.test)
17. Create a Java Class Enter a name for the new class (e.g. MainForm) and make it executable (checking public static void main(String[] args) then press the Finish button
18. Edit the Java Class Fill the MainForm class with the following code public class MainForm { private static int n = 0; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText( "Click counter" ); shell.setBounds(100, 100, 200, 100); shell.setLayout( null ); final Label label = new Label(shell, SWT. PUSH ); label.setBounds(120, 20, 30, 30); final Button button = new Button(shell, SWT. PUSH ); button.setBounds(10, 10, 80, 30); button.setText( "Click Me" ); button.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { n ++; label.setText( "" + n ); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
19. Edit the Java Class In order to solve the errors that are showed: right-click on the editor area and perform a Source, Organize imports operation
20. Edit the Java Class Choose to import the SWT Button and Label class (org.eclipse.swt.widgets.Button and org.eclipse.swt.widgets.Label )
21. Run the Java Class Save the class (File, Save) then Right-click on the editor area and perform a Run as, Java Application
22. Run the Java Class You should see a form with a button and a label showing the number of times the button was clicked
23. Deploy the Application Now that we have developed our (tiny) application how do we deploy it to a client ? First of all there is a requisite: on the client there must be a Java Runtime Environment installed
27. Deploy the Application Browse and enter a name for the exported JAR (e.g. C:empyFirstSWTApp.jar), then press the Finish button
28. Deploy the Application Ok, we are finished! Now just double-click the exported JAR file: it is your executable Java SWT Application. It can copied and launched from any client with a Java Runtime Environment installed!