Hello, World!

Below is the output of the traditional "hello world" application written in Pivot:

This version of the application was written entirely in Java and is shown below; a BXML example is shown in the next section:

            
            package org.apache.pivot.tutorials;

            import java.awt.Color;
            import java.awt.Font;

            import org.apache.pivot.collections.Map;
            import org.apache.pivot.wtk.Application;
            import org.apache.pivot.wtk.Display;
            import org.apache.pivot.wtk.HorizontalAlignment;
            import org.apache.pivot.wtk.Label;
            import org.apache.pivot.wtk.Style;
            import org.apache.pivot.wtk.VerticalAlignment;
            import org.apache.pivot.wtk.Window;

            public class HelloJava implements Application {
                private Window window = null;

                @Override
                public void startup(Display display, Map<String, String> properties) {
                    window = new Window();

                    Label label = new Label();
                    label.setText("Hello World!");
                    label.getStyles().put(Style.font, new Font("Arial", Font.BOLD, 24));
                    label.getStyles().put(Style.color, Color.RED);
                    label.getStyles().put(Style.horizontalAlignment,
                        HorizontalAlignment.CENTER);
                    label.getStyles().put(Style.verticalAlignment,
                        VerticalAlignment.CENTER);

                    window.setContent(label);
                    window.setTitle("Hello World!");
                    window.setMaximized(true);

                    window.open(display);
                }

                @Override
                public boolean shutdown(boolean optional) {
                    if (window != null) {
                        window.close();
                    }

                    return false;
                }

                @Override
                public void suspend() {
                }

                @Override
                public void resume() {
                }
            }
            
        

The program demonstrates some of the fundamental features of the Pivot platform: the Application interface, the Window class, and styles.

The Application Interface

The Application interface is the entry point into a Pivot application. It is similar to the main() method used in C and Java programming or the lifecycle methods used in traditional Java applet development. It defines the following four methods:

  • startup() - called when an application is starting up
  • shutdown() - called when a running application is shutting down
  • suspend() - called when an application is temporarily deactivated
  • resume() - called when a suspended application is resumed

However, unlike main() or the applet lifecycle methods, which require a separate code base for each environment, Application defines a single interface that is used for both web deployment or desktop execution, allowing the same program to run unmodified in either environment.

A Pivot application can be run in the browser using the <applet> tag, as shown below; the class name of the Pivot application is specified by the "application_class_name" applet parameter (see this article for more information on how to deploy Java applets):

            
            <applet code="org.apache.pivot.wtk.BrowserApplicationContext$HostApplet"
                archive="lib/pivot-core-[version].jar,lib/pivot-wtk-[version].jar,lib/pivot-wtk-terra-[version].jar,lib/pivot-tutorials-[version].jar"
                width="160" height="80">
                <param name="application_class_name" value="org.apache.pivot.tutorials.HelloJava">
            </applet>
            
        

The same application can be run from the command line using the following syntax (minus the line breaks) on UNIX-based systems:

java -cp pivot-core-[version].jar:pivot-wtk-[version].jar: pivot-wtk-terra-[version].jar:pivot-tutorials-[version].jar org.apache.pivot.wtk.DesktopApplicationContext org.apache.pivot.tutorials.HelloJava

and the following on Windows systems:

java -cp pivot-core-[version].jar;pivot-wtk-[version].jar; pivot-wtk-terra-[version].jar;pivot-tutorials-[version].jar org.apache.pivot.wtk.DesktopApplicationContext org.apache.pivot.tutorials.HelloJava

Note that (for offline testing purposes) pivot-tutorials-[version].jar is not provided directly in binary distribution, but it's inside generated war files (under the lib folder). Otherwise, you can build it from sources.

The application class name is specified as the first argument to the DesktopApplicationContext loader application.

Note that you can even add the main() method to be able to run the class as Standard Java Application in the usual way.

            
                public static void main(String[] args) {
                    DesktopApplicationContext.main(HelloJava.class, args);
                }
            
        

The Window Class

A window is the top-level entry point into an application's user interface. Almost all Pivot applications will use at least one window.

The window in the sample application is an instance of Window, the most basic window type. It is simply an undecorated area of the screen into which other components may be placed. Other window types, such as Dialog and Frame, add additional features and behaviors such as title bars and modality.

The window used by "Hello World" is "maximized": it automatically fills the entire area of the display. A maximized, decorationless window is commonly used as a top-level application window, particularly for applications that will be primarily run in a web browser. However, windows can also be given an explicit size, can be resized by the user, or can take on the default size of their content. Windows are discussed in more detail in the Windows section.


Styles

Styles are a means of customizing a component's appearance. Style properties are defined by a component's skin and are accessed via a component's styles collection. For example, the example application sets the font, color, and alignment styles on the "Hello World" label. Though skins are not required to provide styling support, most will provide similar capabilities.


Next: Hello, BXML!