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


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 {


     * 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;


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


     * 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.
    public void resume() throws Exception
        // TODO Auto-generated method stub


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

    public void startup(Display display, Map<String, String> map)
           throws Exception
        //Build the Display.

        //Show window.


    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));

        //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.

        //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);

        //Add the BoxPane to the window for display.
        window.setTitle("Hello World!");


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); 

  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: