Events and Sub-windows
|
|||||
Creating
Sub-windows There are two important differences between main windows and sub-windows:
When you close a main window, the application closes; when you
close a sub-window, only it closes. In
addition, when you are using a Droplets Dialog instead of a Frame (which
is not required for sub-windows), your sub-windows cannot be maximized
and minimized. In order to illustrate the concept of sub-windows and to explain how you write sub-windows into your Droplet's application logic we're going to alter the Hello World demo that we built in the last tutorial. As you can see from the image below, we're deleting the main window's two labels and leaving only a button in the center of the window that says "Press This Button Now!":
We've turned our Hello World label into a button and created a ClickListener to handle button clicks;
The easiest way to create an application sub-window is to use the Droplets Window Wizard which is provided with your SDK installation. In your Droplets SDK installation (usually C:\DropletsSdk2.0), go to java\tools and run the file windowwizard.bat. The Droplets Project Setup Wizard will now appear. As you can see from the image below, the project and package names to be associated with this subwindow are already filled in:
The Droplets Window Wizard allows you to customize your new subwindow quickly. Select a class name; choose whether the window will be a Frame or a Dialog (this is based on Java's Frames and Dialogs, see the Droplets API for Java for details of each); assign appropriate text for the window's title bar; and define the window's size.
When you press "OK", your subwindow class will be generated for you and will appear alongside the rest of the classes in your project:
Now you can edit the generated file in the text editor of your choice:
Handling button clicks (and positioning sub-windows)Our sub-window application must handle a user-initiated event, the click of the main window's button. All Droplets components have some variety of ActionListener interface, specific to their attributes, that listens for events; since a button is clicked, its listener is a ClickListener. Important Note: Remember to import the com.droplets.api.event.* package for Droplets that require event handling. There are five items that we'll need from the Droplets API in order to handle any click event: A ClickEvent
that is fired whenever our button is clicked. Here is the code for our ClickListener:
Note first off that in our constructor we pass the ClickListener the current application as an argument. You'll see how to do that when we come to the main window. After defining a constructor and indicating that it takes an Application object as its argument, the actual handling of the click is performed by the handleClick() method. handleClick(ClickEvent e) is a method of the interface ClickListener which we are implementing, and is called by the framework when the button is clicked. In response to a button click, our application pops up a sub-window. While that sub-window is defined within its own class, we do some formatting work here, including the creation of a SubWindow instance; the locating of the sub-window on the user's screen; the insertion of text into the Title Bar; and the placement of the new sub-window into the application (which causes it to appear on the client side). Of these steps, the least immediately obvious is locate(), which is a method which Frame inherits from Window. It takes four arguments; the first two define its location on the user's screen, the second two its absolute size in pixels:
The first two arguments, which define the sub-window's screen position, are both set at -1; this centers the sub-window within the end-user's screen. If you don't want the sub-window centered in the user's window, you'll instead have to indicate the exact location in pixels, which is a bit more complicated. In order to do this you'll have to: Determine the
size of the end-user's computer screen The first step is possible because the Droplets Client always passes this information to the Droplets Server on application start-up as a Variable Value, information about the end-user's computer environment that might be useful for your applications. You can access these Variable Values by calling the Application object's GetVariableValue() method and specifying the height and width of the computer screen. This method has built-in variables called ScreenWidth and ScreenHeight that you can reference to get the exact size in pixels of the end-user's screen. With our demo, it might look something like this:
Once you've created int variables to hold the width and height of the user's screen, you can then position the window relative to the screen size. For example, if you wanted to place the sub-window in the top left corner of the screen, the first two arguments to locate would be (0, 0); if you wanted to place it in the bottom right corner it would be ((screenWidth - 300), (screenHeight - 500), 300, 500), where 300 and 500 are the actual pixel size of the sub-window. Something in between these two extremes will require a bit of experimentation. The ClickListener is associated with the main window's button in the addAllComponents() method of the main window:
The code for associating the ClickListener with its component is invariable (except of course for the two variable names) and can be used pretty much as-is for all applications that involve a listener:
ClickListener is associated with its main window by creating an instance of it within the window's addAllComponents(). Its necessary argument is always provided with a call to the window's getApplication() method. We then call the component's addClickListener() method with the new ClickListener object as an argument.
Feel free to check out the complete code for this demo at Java/Examples/Subwindows within your Droplets installation directory, or move on within the SDK documentation by clicking the Droplet icon at the bottom of this page.   Return to Droplets SDK documentation home. |