Home
.. Links
.. Search
.. Plugins
.. Help
.. Irc Faq

Projects
.. Platform/Faq
.. JDT/Faq/Plan
.. PDE/Faq
.. SWT/Faq
.. RCP/Faq

Tools Projects
.. CDT/Faq
.. GEF/Faq
.. EMF/Faq

Wiki Tutorials

Hosted Projects
.. MTJ
.. Google Summer Of Code 2007
.. Update Manager 2.0
.. EasyEclipse
.. Stylebase for Eclipse

Archives

CreateStandAloneApplication



Post from "lize" describing a very straightforward way to configure SWT for your apps

SWT is similiar to AWT. So the most obvious way is let the SWT integrate
into the JRE.

Follow these steps:

$jre$ is your location of jre

1.copy swt-win32-2034.dll to $jre$/bin
2.copy swt.jar to $jre$/lib/ext
3.add $jre$/bin to your path, remove previous java path setting
4.remove any other java excutables, such as c:windowssystem32java

Then you can run SWT standalone programs just as any java programs.

This suggestion is based on two knowledge points from Java:

1.How System.loadLibrary() finds the native library.
1)You can copy the swt-win32-2034.dll to $jre$/bin
2)You can also copy the swt-win32-2034.dll to c:windowssystem32 to
obtain the same effect
3)you can set -Djava.library.path when starting a SWT program

2.How Java bootstrap loader finds the class libraries.
If you add a jar file to $jre$/lib/ext, it will be automatically
detected. So if you frequently program using SWT, putting swt.jar in
$jre$/lib/ext is more convenient than adding it to each project's classpath.


Post from Peter Manahan explaining how SWT can be accessed in a non-Eclipse batch script

For Windows I needed to add to the path plugins/org.eclipse.swt/ws/win32 to
pick up the SWT dll.
For Linux i added the path to libXm.so (its in the root dir of Eclipse) and
to plugins/org.eclipse.swt/ws/motif to the LD_LIBRARY_PATH


Here are examples:
runnit.bat on Windows
set ECL_HOME=f:ecl137eclipse
set path=%ECL_HOME%pluginsorg.eclipse.swtwswin32
java -classpath
.;%ECL_HOME%pluginsorg.eclipse.swtswt.jar;%ECL_HOME%pluginsorg.eclipse.uiworkbench.jar;xerces.jar
MyCoolTool
runnit.sh on Linux
export ECL_HOME=/opt/wsappdev
export
LD_LIBRARY_PATH=${ECL_HOME}:${ECL_HOME}/plugins/org.eclipse.swt/ws/motif:${LD_LIBRARY_PATH:-/usr/lib:/lib}

/opt/IBMJava2-13/bin/java -classpath
.:${ECL_HOME}/plugins/org.eclipse.swt/swt.jar:${ECL_HOME}/plugins/org.eclipse.ui/workbench.jar:${ECL_HOME}/org.apache.xerces/xerces.jar
MyCoolTool


Run a standalone SWT app from Eclipse

Instructions for F1 build on Win32. Replace ECLIPSE with the path of your eclipse folder.

1. Create a new Java project.
2. Add swt.jar to build path.

Right-click on project name in Navigator or Packages view and select Properties... Select Java Build Path / Libraries. Click Add External Libraries and add ECLIPSE_HOMEpluginsorg.eclipse.swt.win32_2.0.0wswin32swt.jar.

It also helps to attach Java source to SWT library.
Right-click on SWT library in Packages view and select "Properties".
Then select "Java Source Attachment" end enter source file name:
ECLIPSE_HOMEpluginsorg.eclipse.platform.win32.source_2.0.0srcorg.eclipse.swt.win32_2.0.0wswin32swtsrc.zip

This will give pop-up help and ability to examine source with F3 key, as described in Hacking on Eclipse Itself

3. Create a File / New / Class with a main() method. E.g.,
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Standalone {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(100,100,150,50);
shell.setText("Hi");
shell.setLayout(new GridLayout());
Label label = new Label(shell, SWT.CENTER);
label.setText("Hello, world!");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

4. Add swt-win32-2040.dll to Java path.

Click on down arrow beside running man icon. Select Run As / Local Java Application... In dialog that appears, select Arguments tab. In the VM Arguments text box, enter:

-Djava.library.path=ECLIPSEpluginsorg.eclipse.swt.win32_2.0.0oswin32x86

5. Click Run button.

Window with title "Hi" containing text "Hello, world!" should appear.

create a stand-alone swt application

You can find here an example of using SWT in a stand-alone mode. Unfortunately, the text is written in French but I think the code is quite readable (using English convention).

Google might help you translating the stuff. Try this for an English translation of the page. It works amaising well... :-)

Tips to create Mac OS X SWT applications.



create a stand-alone jface application


You can find an example about how to create a stand-alone jface application.

(Mise à jour le 25/03/2002)
Auteur : Xavier MEHAUT

Steps :
Modify project properties

Create a class

Pour créer une application, il est nécessaire de faire une sous-classe de la classe ApplicationWindow de la manière suivante :

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

public class TestJFace extends ApplicationWindow {
public TestJFace(Shell parentShell) {
super (parentShell);
}
public static void main(String[] args) {
Display display =
new Display();
Shell aShell =
new Shell(display);
TestJFace application =
new TestJFace(aShell);
application.open();
}
}

Redefined methods

To widen the functionnality numbers we must redefine the methods already defined in ApplicationWindow or in its super-classes Window.

  • protected Control createContents(Composite parent) : called when creating misc. window components like status bar, tool bar, menu bar, composites, ...
  • protected void handleShellCloseEvent() : action when closing the window
  • protected Point getInitialSize() : initialize the size of the window
  • protected Point getInitialLocation() : initialize the window location
  • protected void configureShell(Shell shell): to initialize the application
  • protected void createMenuManager() : to redefine the menu
  • protected void createToolManager(int i) : to define the toolbar
  • protected void createStatusLineManager() : to define the status bar
JFace example


package example;


import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.MenuItem;
import
org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;


public class TestJFace extends ApplicationWindow {

public TestJFace(Shell parentShell) {
super (parentShell);
}

public static void main(String[] args) {
Display display = new Display();
Shell aShell = new Shell(display);
TestJFace application = new TestJFace(aShell);
application.createComponents();
application.open();
application.setStatus( "sdqds" );
}

public void createComponents() {
addMenuBar();
addStatusLine();
addToolBar(SWT.FLAT | SWT.WRAP);
setBlockOnOpen( true );
};

protected MenuManager createMenuManager() {
MenuManager menuManager = new MenuManager();
menuManager.add(createHelpMenu());
return menuManager;
}

private MenuManager createHelpMenu() {
MenuManager menu = new MenuManager( "Help" , "Id01" );
menu.add( new GroupMarker( "About" ));
menu.add(createAboutAction());
return menu;
}

private Action createAboutAction() {
return new Action() {
public String getText() {
return "Essai" ;
}

public void run() {
String[] tab = { IDialogConstants.OK_LABEL };
MessageDialog dialog =
new MessageDialog(
getShell(),
"Essai JFace" ,
null ,
"Ceci est une fenêtre d''information" ,
MessageDialog.INFORMATION,
tab,
0);
dialog.open();
}
};
}

protected void configureShell(Shell shell) {
super .configureShell(shell);
shell.setText( "Essai JFace" );
}

protected Point getInitialSize() {
return new Point(600, 400);
}

protected Point getInitialLocation() {
return new Point(600, 400);
}

protected void handleShellCloseEvent() {
String[] tab = { IDialogConstants.OK_LABEL };
MessageDialog dialog =
new MessageDialog(
getShell(),
"Fin de l'essai" ,
null ,
"Nous allons quitter l'application" ,
MessageDialog.INFORMATION,
tab,
0);
dialog.open();
setReturnCode(CANCEL);
close();
}

protected ToolBarManager createToolBarManager( int style) {
return new ToolBarManager(style);
}

protected Control createContents(Composite parent) {
Table aComposite = new Table(parent, SWT.BORDER);
return aComposite;
}

}


Comments:

From jtf - 8/23/05 4:43 PM

I had trouble finding directions on creating a headless application so I put together a set of pictorial directions:

 http://www.developertesting.com/archives/month200508/20050823-HeadlessHelloWorldInEclipse.html

From SilvioMoioli - 8/20/05 4:01 AM

Silvio Moioli has created an utility to pack all the needed native libraries directly in the Jar file.

That way you can get a stand-alone complete SWT application in a plain double-clickable file (no scripts nor installers needed!).

The utility is named SWTLoader and can be found at: http://www.moioli.net/art29.html

Feedback is welcome: silvio at moioli dot net



Last Modified 6/21/06 10:23 AM

Hide Tools