Sample: BeanDemo
Objectives
This sample illustrates how to build an application using visual composition.
Before you begin
Classes sunw.demo.molecule.Molecule and sunw.demo.juggler.Juggler must
be loaded before editing or running this application. You can do
this by importing their jar files.
Description
This sample illustrates using features of visual composition for the following
tasks:
Running the BeanDemo Sample
The BeanDemo sample includes a bean that demonstrates using beans inside
beans. It incorporates the Molecule Viewer and the Juggler applet
from the Sun BDK inside a Frame.
To run the sample:
-
Select the BeanDemo class from the Projects page.
-
Select the Run button on the tool bar.
The view of the molecule bean can be rotated by pressing the left mouse
button and moving the cursor. The type of molecule being viewed can be
selected from the choice list.
The juggler animation can be started and stopped by pressing the appropriate
button.
To build the BeanDemo Sample
There is only one bean in the beandemo package and it is called BeanDemo.
Follow the build instructions below.
Building the BeanDemo class
-
Create the BeanDemo class as follows:
-
From the Projects page of the Workbench
-
Select the package where you will save this sample
-
Select the Create Class button on the tool bar
-
Class name = BeanDemo
-
Superclass = java.awt.Frame
-
Select the Browse the class when finished check box
-
Select the Compose the class visually check box
-
Click Finish
-
The Visual Composition page of the class browser
will be displayed, where you can build the user interface as follows:
-
Open the property sheet for the ContentsPane in BeanDemo
-
Select the Beans List button on the tool bar
-
Double-click on ContentsPane
-
Set properties for ContentsPane
-
Switch to the AWT category in the palette
-
Drop a Panel on the free-form surface. Set properties for Panel1
-
From the palette select Choose Bean
-
Bean Type = Class
-
Class Name = sunw.demo.molecule.Molecule
-
Drop the bean in the Center portion of Panel1 (holding down mouse button
1 with the loaded bean will make the layout visible and make positioning
the bean in the center easier).
-
(make sure constraints = Center)
-
Drop a Choice bean in the South portion of Panel1 (this can be done from
the Beans List)
-
(make sure constraints = South)
-
In the Beans List, move Panel1 into the ContentsPane of BeanDemo. Set
properties for Panel1
-
Drop a Button in the Center portion of ContentsPane. Set properties for
Button1
-
font => Size = 10
-
label = Exit the bean demo
-
(make sure the constraints = Center)
-
Drop a Panel on the free-form surface. Set properties for Panel2
-
Drop a Button in the North portion of Panel2. Set properties for Button2
-
From the palette select Choose Bean
-
Bean Type = Class
-
Class Name = sunw.demo.juggler.Juggler
-
Drop the bean in the Center portion of Panel2
-
(Note: You will not see anything visible where Juggler
is placed. Juggler is an Applet, and to be visible it has to be running.
Applets are not started in the Visual Composition Editor.)
-
Drop a Button in the South portion of Panel2. Set properties for Button3
-
In the Beans List, move Panel2 into the ContentsPane of BeanDemo.
Set properties for Panel2
-
Add nonvisual beans:
-
From the palette select Choose Bean
-
Bean Type = Class
-
Class Name = sunw.demo.molecule.MoleculeNameEditor
-
Drop the bean on the free-form surface below BeanDemo
-
From the palette select Choose Bean
-
Bean Type = Class
-
Class Name = java.lang.Runtime
-
Drop the bean on the free-form surface below BeanDemo
-
Save the bean and generate code:
-
Select Bean from the menu
-
Select Save Bean
-
Make connections to set properties and invoke methods as follows (see Connection
Syntax ):
-
Event-to-Code connection
-
Source: BeanDemo windowOpened/event
-
Target: CODE fillChoice1/method
Method fillChoice1 will initialize the Choice bean with a list of Molecule
bean names. To make this connection:
-
Locate the BeanDemo class provided with the product samples
-
Copy its code for the fillChoice1 method to the clipboard
-
Select the BeanDemo windowOpened event as the source for the connection
-
Click on the free-form surface
-
Select Event to Code
-
Delete the source code and replace it with the fillChoice1 method source
code from the clipboard
-
Use Ctrl+S to save the method
-
Deselect the Pass event data checkbox
-
Click OK
-
Method parameter: sunw.demo.molecule.MoleculeNameEditor
-
Parameter source: aMoleculeNameEditor
-
Parameter target: MoleculeNameEditor1 this/property
When the windowOpened event occurs for BeanDemo, the fillChoice1 method
will be invoked.
-
Event-to-method connection
-
Source: Choice1 itemStateChanged/event
-
Target: Molecule1 moleculeName/property
-
Method parameter: java.lang.String
-
Parameter source: value
-
Parameter target: Choice1 selectedItem/property
When the itemStateChanged event occurs for Choice1, the setMoleculeName
method is invoked for Molecule1. The moleculeName property is set
to the selectedItem property of Choice1.
-
Event-to-method connection
-
Source: Button1 actionPerformed/event
-
Target: Runtime1 exit/method
-
Method parameter: int
-
Set Parameter: status = 0
When Button1 is pressed, the exit method will be invoked with the status
parameter value set to 0.
-
Event-to-method connection
-
Source: Button2 actionPerformed/event
-
Target: Juggler1 startJuggling/method
When Button2 is pressed, the startJuggling method will be invoked for
Juggler1.
-
Event-to-method connection
-
Source: Button3 actionPerformed/event
-
Target: Juggler1 stopJuggling/method
When Button3 is pressed, the stopJuggling method will be invoked for
Juggler1.
Save the bean, and the sample is ready to run!