Hello, BXML!

Below is the output of the "hello world" application written using BXML. Note that, with the exception of the actual text, it is identical to the output of the Java version:

The following is the Java source code for the BXML version. The content of the user interface is defined entirely by the BXML source file. It is loaded using an instance of the BXMLSerializer class defined in the org.apache.pivot.bxml package.

            package org.apache.pivot.tutorials;

            import org.apache.pivot.beans.BXMLSerializer;
            import org.apache.pivot.collections.Map;
            import org.apache.pivot.wtk.Application;
            import org.apache.pivot.wtk.Display;
            import org.apache.pivot.wtk.Window;

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

                public void startup(Display display, Map<String, String> properties)
                    throws Exception {
                    BXMLSerializer bxmlSerializer = new BXMLSerializer();
                    window = (Window)bxmlSerializer.readObject(HelloBXML.class, "hello.bxml");

                public boolean shutdown(boolean optional) {
                    if (window != null) {

                    return false;

                public void suspend() {

                public void resume() {

The BXML that is used to create the UI is shown below:

            <Window title="Hello BXML!" maximized="true"
                <Label text="Hello BXML!"
                    styles="{font:'Arial bold 24', color:'#ff0000',
                        horizontalAlignment:'center', verticalAlignment:'center'}"/>

You can run this example as a Standard Java Application executing ScriptApplication in the following way:
org.apache.pivot.wtk.ScriptApplication --src=/org/apache/pivot/tutorials/hello.bxml

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(HelloBXML.class, args);

Most Pivot applications will be constructed this way, declaring the structure of the UI in BXML rather than creating it programmatically in code. However, event handlers, which allow an application to respond to user input, are always defined in code (either Java or a compatible JVM scripting language). For example, the following BXML produces output very similar to the previous two versions, using a combination of BXML and JavaScript:

            <Window title="Hello JavaScript!" maximized="true"

                    function windowOpened(window) {
                        var label = new Label();
                        label.setText("Hello JavaScript!");
                        label.getStyles().put("font", "Arial BOLD 24");
                        label.getStyles().put("color", "#ff0000");
                        label.getStyles().put("horizontalAlignment", HorizontalAlignment.CENTER);
                        label.getStyles().put("verticalAlignment", VerticalAlignment.CENTER);


The output of this version is shown below:

Note that this example does not provide an implementation of the Application interface. Rather, it is launched using the org.apache.pivot.wtk.ScriptApplication class. This class, which is included with the Pivot platform, implements the Application interface and takes the name of a BXML file to launch as a startup property named src. The source file is expected to contain a root element representing a Window subclass.

You can run this example as a Standard Java Application executing ScriptApplication in the following way:
org.apache.pivot.wtk.ScriptApplication --src=/org/apache/pivot/tutorials/hello_javascript.bxml

The class is named ScriptApplication because it is often used to launch applications whose logic is defined entirely in script; however, it can actually be used to launch any application whose main window is defined in a single BXML file. Many of the examples in the following sections are executed using this class. Scripting is discussed in more detail in the Scripting section.

Accessing BXML resources is the same as accessing any other kind of resources from within a Java application, following the same rules of standard ClassLoader resource retrieval methods.
Note that it's not mandatory to use BXML files within Pivot (all could be done in Java or any JVM compatible language), but BXML is a much more convenient method for specifying the UI layout.

Next: Component & Container