Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Apache Pivot. Making Java GUI is easy.

Posted on 22/01/2010

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

  • Pivot website
  • Project – HTML Image maker

1 thought on “Apache Pivot. Making Java GUI is easy.”

  1. Pradeep says:
    24/09/2018 at 11:49

    how will you test the app?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
6dc4b508c301309b9e838ba10393207a-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme