Apache Pivot Label

//Create a new Apache Pivot Label
Label label = new Label();

//Set the Font:
label.getStyles().put(“font”, new Font(“Verdana”, Font.BOLD, 24);

//Set Text Color
label.getStyles().put(“color”, Color.WHITE);

//Set Horizontal Alignment of the Text within the Label.
//Options include: LEFT, RIGHT, CENTER

label.getStyles().put(“horizontalAlignment”, HorizontalAlignment.CENTER);

//Set Vertical Alignment of the Text within the Label.
//Options include: TOP, BOTTOM, CENTER

label.getStyles().put(“verticalAlignment”, VerticalAlignment.CENTER);

//Set the Background color
label.getStyles().put(“backgroundColor”, Color.RED);

//Set the Padding on all four sides (top, right, bottom, left) to 
//
same number of pixels. In next line = 10 pixels.
label.getStyles().put(“padding”, 10);

//Set the Padding on each side specifically:
label.getStyles().put(“padding”, “{top:5, left:15, bottom:25, right:35}”);

For example:

Apache Pivot Label Example

Apache Pivot Label Example

Labels.java

package samples.labels;

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

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.*;

public class Labels implements Application {

    //************************
    //Attributes
    //************************

    /*
     * This the most basic means by which content may be
     * placed on the screen. It simply provides a blank  
     * canvas in which other components may be placed.
     * In our example, we add the BoxPane to the window.  
     */
    private Window window;

    /*
     * Box panes are used to arrange components. Components
     * added to box panes can be horizontal or vertically
     * stacked.
     */
    private BoxPane boxPane;

    //************************
    //Constructors
    //************************

    /**
     * Instantiate our window and box pane
     * in the default constructor.
     */
    public Labels()
    {
        window = new Window();
        boxPane = new BoxPane();
    }

    //************************
    //Methods
    //************************

    /**
     * The Application interface requires program
     * to override four methods:
     * suspend, resume, startup, shutdown.
     *  
     * An interface can be thought of as a contract.
     * The Application interface promises to perform
     * these services if your program adheres to
     * the requirements.
     *
     * For example, when this program implements
     * the Application interface, it expects to find
     * these four methods. And you code within these
     * methods will be executed.
     */
    @Override
    public void resume() throws Exception
    {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean shutdown(boolean arg0) throws Exception
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void startup(Display display, Map<String, String> map)
           throws Exception
    {
        //Build the Display.
        buildDisplay(display);

        //Show window.
        window.open(display);

    }

    @Override
    public void suspend() throws Exception {
        // TODO Auto-generated method stub

    }

    private void buildDisplay(Display display)
    {
        //Set boxPane to center of screen. Apply a background color,
        //Then set a vertical orientation (so labels stack).
        boxPane.getStyles().put("verticalAlignment", "CENTER");
        boxPane.getStyles().put("horizontalAlignment", "CENTER");
        boxPane.getStyles().put("backgroundColor", new Color(237,217,185));
        boxPane.setOrientation(Orientation.VERTICAL);

        //Create a label using 1-argument constructor
        Label helloLabel = new Label("Hello World!");

        //Set label font.
        helloLabel.getStyles().put("font", new Font("Verdana", Font.PLAIN, 16));

        //Set label font color.
        helloLabel.getStyles().put("color", new Color(134, 107, 64));

        //Add the Label to the BoxPane Layout.
        boxPane.add(helloLabel);

        //you can also use the default constructor.
        Label goodByeLabel = new Label();
        goodByeLabel.setText("Good bye!");
        goodByeLabel.getStyles().put("font", new Font("Times", Font.BOLD, 24));
        goodByeLabel.getStyles().put("color", Color.BLACK);
        boxPane.add(goodByeLabel);

        //Add the BoxPane to the window for display.
        window.setContent(boxPane);
        window.setTitle("Hello World!");
        window.setMaximized(true);
    }
}

Driver.java

package samples.labels;

import org.apache.pivot.wtk.DesktopApplicationContext;

public class Driver {

	//Entry point into all java applications.
	public static void main(String[] args) throws Exception
	{
		/**
		 * Load the pivot application into our Java program.
		 */
		DesktopApplicationContext.main(Labels.class, args); 

	}
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: