Tuesday, February 25, 2014

JavaFX using cascading style sheet (CSS) to change the appearance of your GUI

Using CSS to control the appearance of the user interface in JavaFX is quite similar to using CSS to control the user interface in HTML.


Style Definitions

Create the <your file name>.css file and put it in the main directory of your JavaFX application. Each style definitions in the CSS file has a name also known as the selector, followed by the curly braces {} that defines the declaration block inside which are the rules of the style definition. Each rule is a property with a value assigned to it. If the selector of the style definition is associated with a class name, it is preceded by a dot, and is known as a class style selector. For example,

.button{
       -fx-font: 18px "Serif";
       -fx-text-fill: white;
       -fx-alignment: LEFT;
       -fx-padding: 10;
       -fx-foreground-color: #00FF99;
       -fx-background-color: #CCFF99;
}

 If the selector of a style definition is associated with a JavaFX node ID, the selector is preceded by the # sign, and is known as an ID style selector. the node ID can be set by calling the node's setID() method. For example, if you have set the node ID of your Save button to save-button, the style definition may look like this.

#save-button{
       -fx-font-size: 14px;
       -fx-font-family: "Courier New";
       -fx-text-fill: rgb(203, 128, 99);
       -fx-alignment: CENTER;
       -fx-padding: 12;
}


.root Style Class

The .root style class defines the appearance of the root node of the scene. Since all nodes in the scene are descendants of the root node, rules in the .root style class are also applied to all the other nodes in the scene. A style rule assigned to a particular node overrides the corresponding rule in the .root style class. The common rules that consistently apply to other nodes are also included in the .root style class. They can be referenced by other style definitions to ensure UI consistency. For example in the following .root style class, the -fx-base is such a property that can be used by other style definitions.

.root {
       -fx-focused-base: blue;
       -fx-background: rgb(225, 212, 205);
}

.text-field.focused {
      -fx-color: -fx-focused-base;
}


Assign a class style definition to your control

Button saveButton = new Button("Save");
saveButton.getStyleClass().add( "<the class selector of your style definition>" );


Assing an ID style definition to your control

Button saveButton = new Button("Save");
saveButton.setId( "<the ID selector of your style definition>" );


Inline styles in the code

Instead of using the style definitions in a separated .css file, you can add your styles directly in the code.

Button saveButton = new Button("Save");
saveButton.setStyle( "-fx-font-size: 14px; -fx-font-family: "Courier"; -fx-alignment: CENTER;" );
          
--------------------------------------------------------------


                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment