## Metagnostic home
| # JNIPort for Dolphin Smalltalk
Overview Contents Players Layers Examples Configuration InFAQ Changes Licence Back to Goodies | # Ghost-class Examples This section is an example of using ghost classes to create and display a Swing window. JNIPort should be configured to use ghost classes and support callbacks. To configure ghost classes, ensure that the list of 'watcherClasses' under 'jniPortSettings' includes `JVMGhostClassMaker`, there is no need to change the 'ghostClassSettings' from their defaults. Since AWT/Swing uses Java threads, you should make sure that the JNI Helper is installed, or that you are not hooking the Java runtime's debugging output. If you followed the instructions in the installation walkthrough then JNI Helper will be configured. Ensure that a properly configured JMV is running, and open a new workspace. We start, as always, by getting a reference to the JVM object:
    	jvm := JVM current.
First we make an instance of `javax.swing.JFrame`. The first time you do this in any one session will cause a pause of up to a minute (depending on the speed of your machine) as JNIPort imports nearly 300 Java classes and creates corresponding ghost classes. A later version of JNIPort will use a lazier import scheme to mitigate this startup delay (Stop press: actually that is available now, if you use a `JVMLazyGhostClassMaker` instead of the `JVMGhostClassMaker` mentioned above — the feature is still experimental, though, so we don't use it in this example).
    	frame := (jvm findClass: #'javax.swing.JFrame')
    			new_String: 'JNIPort!'.
Now, we make sure that the frame won't call `java.lang.System.exit()` when it is closed. If it did then it would close our Dolphin session too! Notice how we are using the frame's class static to find the Java class constant `DISPOSE_ON_CLOSE`.
    	closeOp := frame static get_DISPOSE_ON_CLOSE.
    	frame setDefaultCloseOperation_int: closeOp.
Now we ask the frame to display itself (calling `pack()` then `setVisible(true)` is a standard AWT/Swing idiom):
    	frame pack_null; setVisible_boolean: true.
We can hide the frame:
    	frame setVisible_boolean: false.
And show it again:
    	frame setVisible_boolean: true.
Now we'll create a scrollable panel that will display a picture (chose some handy JPEG file). Notice how the delay is much shorter this time; much of Swing was already loaded as a result of finding `javax.swing.JFrame`.
    	picture := FileOpenDialog showModal.
    	icon := (jvm findClass: #'javax.swing.ImageIcon')
    			new_String: picture.
    	label := (jvm findClass: #'javax.swing.JLabel')
    			new_Icon: icon.
    	pane := (jvm findClass: #'javax.swing.JScrollPane')
    			new_Component: label.
Add the new pane to the frame and ask the frame to lay itself out again, which should display the picture:
    	frame getContentPane_null add_Component: pane.
    	frame pack_null.
And remove the pane:
    	frame getContentPane_null remove_Component: pane.
    	frame pack_null.
Close the frame:
    	frame dispose_null.

Copyright © Chris Uppal, 2003-2005

Java, JNI (probably), JVM (possibly), and God knows what else, are trademarks of Sun Microsystems, Inc.