Thursday, January 8, 2009

Java - GUI development using Eclipse IDE




under eclipse Java is using SWT for GUI development.

http://www.eclipse.org/swt/

Following is a small sample code to demostrate how to use SWT under eclipse to develop GUI using java. Also, it tells how to run your java class under ms-dos command line.

==== CablePowerView.java =============

import org.eclipse.swt.widgets.*;

public class CablePowerView {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Cable Power Viewer Started!");
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Swt working!");
shell. setSize(500, 375);
shell.open();
while(!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep();
display.dispose();
}
}
====== antbuild.xml ===========

Screen shot for antbuild.xml is attached as *.xml can not be displayed properly in the blog.

====== Run it in command line ============

1. From http://www.eclipse.org/swt/
We download SWT Stable Version for Windows (3.4 Final Release - 19 June 2008) swt-3.4-win32-win32-x86.zip
unzip we get swt.jar, then unzip swt.jar we get swt-win32-3448.dll.
2. creat run.bat as follows:
chdir .\CablePowerView\build
java -classpath .\swt.jar; -Djava.library.path=.\ CablePowerView
(Note: swt.jar, swt-win32-3448.dll are stored together with CalbePowerView.class under .\CablePowerView\build
run.bat is stored at root directory
.\ -Djava.library.path=.\ use to tell where swt-win32-3448.dll stored.)
Ms-Dos> run.bat
you will see the result that one window popped up. : - )

No comments: