Using JavaFX CSS to Apply Styling to the User InterfaceJavaFX is a platform and language for creating Rich Internet Applications (RIA) that execute on the desktop, in Web browsers, on mobile phones, and on TV. It was introduced in 2007, and version 1.3 was released in April 2010. One of the hallmark features of JavaFX 1.3 is the introduction of JavaFX CSS, which provides the ability to apply styling to any element in the user interface. This article focuses on JavaFX Cascading Style Sheets (CSS), which is primarily the domain of designers but is also important for developers to understand. One of the strengths of JavaFX is that it enables designers and developers to collaborate on creating applications. JavaFX CSS is a feature of JavaFX that encourages this collaboration. As with traditional Web applications, developers can focus on an application's functionality while designers can apply styles to elements of the user interface. For example, Figure 1 contains a screen capture of some JavaFX UI controls using their default styles in a desktop application. ![]() Figure 1: Various JavaFX UI Controls with Their Default Styles
Without changing any JavaFX code, but rather making modifications to the style sheets, the designer can re-style the user interface elements (nodes in JavaFX parlance) of an application. Figure 2 contains screen captures of the same UI controls shown in Figure 1 with different CSS style sheets applied. These screen captures are from the StyleEditor example available on the JavaFX.com site at http://javafx.com/samples/StyleEditor/index.html. ![]() Figure 2: JavaFX UI Controls from Figure 1 with Custom Styles Applied
Another benefit of JavaFX CSS is that style sheets can be dynamically assigned to the scene at runtime. This allows the appearance of an application to be profoundly altered based on the display footprint of the device or user choice. Using JavaFX CSS to Style a Label ControlCSS styles can be applied to any node in the scene graph, including controls, layout managers, shapes, and charts. Figure 3 contains a screen capture of an example program that demonstrates how to apply CSS styling to a Label control. ![]() Figure 3: Screen Capture of the StylingExample Program
The label consists of both the JavaFX logo and the "CSS Rocks!" text. As shown in Figure 4, when the user hovers over the label, the color and style of the label's text changes. In addition, the mouse cursor changes to a hand icon. ![]() Figure 4: The StylingExample Program When the User Hovers over the Label
Obtaining and Running the StylingExample ProgramYou can download a NetBeans project that includes the StylingExample program. After expanding it into a directory of your choice, start NetBeans, and select the File -> Open Project menu. The Open Project dialog should appear, from which you can navigate to your chosen directory and open the StylingExample project, as shown in Figure 5. Note: You can obtain the NetBeans IDE 6.9 for JavaFX 1.3 at the JavaFX.com download site: http://javafx.com/downloads. ![]() Figure 5: Opening the StylingExample Project in NetBeans
To run the application, click the Run Project button on the toolbar, or press the F6 key. The Run Project icon has the appearance of the Play button on a DVD player, as shown in Figure 6. Figure 6: Invoking the StylingExample Program in NetBeans
The StylingExample application should appear in a window, as shown previously in Figure 3. Hover over the icon or text in the label, noting that its appearance changes to the screen capture shown in Figure 4. Understanding the StylingExample ProgramTo open the source code for this program, expand the Source Packages and stylingexample nodes located in the NetBeans Projects pane. You can then double-click the StylingExample.fx node, as shown in Figure 7. ![]() Figure 7: Viewing the StylingExample.fx Source Code in NetBeans
To fully understand how JavaFX CSS styling is being applied to the Label control in this example, we'll walk through the StylingExample.fx source code in Listing 1 as well as the mystyle.css file in Listing 2. Listing 1: StylingExample.fx
package stylingexample;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.*;
Stage {
scene: Scene {
stylesheets: "{__DIR__}mystyle.css"
width: 220
height: 140
content: [
Label {
layoutX: 10
layoutY: 10
id: "mylabel"
graphic: ImageView {
image: Image {
url: "{__DIR__}javafx-logo-154x64.png"
}
}
}
]
}
}
Associating a Style Sheet with the SceneIn Listing 1, the Note: The Assigning an ID to a Node for Reference from a Style SheetAs you'll see when examining the style sheet, two ways of referring to the label from our style sheet are employed in this example. One of these ways depends upon assigning a value to the id: "mylabel" The While we're looking at the label, notice that the
Label {
layoutX: 10
layoutY: 10
text: "CSS Rocks!"
textFill: Color.web("#e76f00");
font: Font.font("Arial", FontWeight.BOLD, 36)
graphicHPos: HPos.CENTER
graphicVPos: VPos.TOP
graphicTextGap: 2
graphic: ImageView {
image: Image {
url: "{__DIR__}javafx-logo-154x64.png"
}
}
}
CSS Rules, Selectors, Properties, and TypesNow that the scene has the mystyle.css style sheet associated with it, and the label has a unique Listing 2: mystyle.css
.scene {
-fx-font: bold 36pt "Arial";
-my-hover-color: #53829e;
}
.label {
-fx-text-fill: #e76f00;
}
.label:hover {
-fx-text-fill: -my-hover-color;
-fx-font-style: italic;
-fx-cursor: hand;
}
#mylabel {
-fx-text: "CSS Rocks!";
-fx-graphic-hpos: center;
-fx-graphic-vpos: top;
-fx-graphic-text-gap: 2px;
}
Each of the four blocks in Listing 2 are known as CSS rules, with each rule having a selector and one or more properties. Each property has a type and is assigned a value of that type. Using the
|
Jim Weaver is an author, speaker, teacher, and developer in rich internet application technologies such as JavaFX, and may be contacted at jim.weaver@javafxpert.com
| |||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||