Apache Pivot. Making Java GUI is easy.

I just got to know to Pivot and I have to say, I love it.

Pivot is a technology to create Java applets for RIA or desktop applications with the use of XML and/or code.

When I write applications at home I don’t want waste time writing swing or applets myself. Pivot helps me designing the GUI with use of XML. It is that simple and the cool thing is that you can run it as a web page and a desktop application.



An example of a Pivot XML (WTKX):

<Window title="Hello" maximized="true"
    xmlns:wtkx="http://pivot.apache.org/wtkx"
    xmlns="org.apache.pivot.wtk">
    <content>
    	<BoxPane styles="{padding:4, horizontalAlignment:'center', verticalAlignment:'center'}">
       		 <Label wtkx:id="label1" text="Please enter your name"
          	  styles="{font:'Arial 20', color:'#ff0000',
              	  horizontalAlignment:'center', verticalAlignment:'center'}"/>
              <TextInput wtkx:id="text1"  /> 
               <PushButton wtkx:id="button1" buttonData="Enter"/>
         </BoxPane>
    </content>
</Window>



After creating the XML, you can refer to the GUI components from the code.

 

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Alert;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.MessageType;
import org.apache.pivot.wtk.PushButton;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;

public class HelloWTKX implements Application {
    private Window window = null;
    private Label label;
    private PushButton button1;
    private TextInput text1;

    @Override
    public void startup(Display display, Map<String, String> properties)
        throws Exception {
        WTKXSerializer wtkxSerializer = new WTKXSerializer();
        window = (Window)wtkxSerializer.readObject(this, "hello.wtkx");
        label = (Label)wtkxSerializer.get("label1");
        button1 = (PushButton)wtkxSerializer.get("button1");
        text1 = (TextInput)wtkxSerializer.get("text1");
        
        button1.getButtonPressListeners().add(new ButtonPressListener() {
                          @Override
                          public void buttonPressed(Button button) {
                             Alert.alert(MessageType.INFO, "Hello " + text1.getText(),
                                     window);
                         }
                     });
        window.open(display);
    }

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

        return false;
    }

    @Override
    public void suspend() {
    }

    @Override
    public void resume() {
    }

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



Actually the entire GUI can be built just from the code, but I like the XML way better.

The application can be run from the IDE or can be referred from a web page as an applet.
All you have to do is refer to the Pivot jars + your jar and give the full name of your application class you have created.


<applet code="org.apache.pivot.wtk.BrowserApplicationContext$HostApplet"
    archive="lib/test.jar,lib/pivot-core-1.4.jar,lib/pivot-wtk-1.4.jar,lib/pivot-wtk-terra-1.4.jar,lib/pivot-tutorials-1.4.jar"
    width="500" height="400">
    <param name="application_class_name" value="HelloWTKX"> <!-- your application class package -->
</applet>




The result:

The Pivot web site contains loads of examples with the use of their components.

downloaddownload sample project for Eclipse but don’t forget also to download Pivot jars.



See also

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>