lab s313674 build a glassfish javafx monitoring
play

Lab-S313674 : Build a GlassFish JavaFX Monitoring Application using - PDF document

Lab-S313674 : Build a GlassFish JavaFX Monitoring Application using REST monitoring API David Delabasse Senior Solution Architect, Oracle Belgium Sbastien Stormacq Senior Solution Architect, Oracle Luxembourg JavaOne|Oracle Develop Build


  1. Lab-S313674 : Build a GlassFish JavaFX Monitoring Application using REST monitoring API David Delabassée Senior Solution Architect, Oracle Belgium Sébastien Stormacq Senior Solution Architect, Oracle Luxembourg

  2. JavaOne|Oracle Develop Build a GlassFish JavaFX Monitoring Application using REST monitoring API Hands-on Lab: S313674 Introduction Building Rich Internet Applications using the traditional Java SE™ platform is a cumbersome task, often reserved to the most seasoned Java™ developers, those mastering Java 2D and Java 3D APIs. JavaFX, introduced in December 2008, is changing the rules of the game. JavaFX provides Java™ developers and graphic designers alike a powerfull scripting language and graphic API, allowing developers to build rich graphical applications very easily. But JavaFX alone will only help to build a graphical front end, while most business oriented applications will require some backend services to provide the data to the end user. Efficient web services communication will therefore be a requirement to any business JavaFX project. This lab introduces web communications between JavaFX graphical applications and server based web services. This lab uses GlassFish's RESTful web service monitoring API on the backend, JSON as the data format to be exchanged between the web service and the graphical application and Jersey (JAX-RS) API as REST API to be used from JavaFX. Prerequisites This hands-on lab assumes you have basic experience with the following technologies: • Java™ language programming • Basic JavaFX understanding, i.e. this lab will not teach you the basic of the JavaFX syntax, the Scene graph etc ... You can refer to the officials JavaFX Tutorials to help you to start with JavaFX. Notations Use In This Documentation The directory into which the lab contents were placed, i.e. where this document lives <lab_root> <netbeans_project> The directory where the NetBeans project is created. By default, this $HOME/NetBeansProjects/S313674_JavaFX_GF_Monitoring unless you will specify a different directory and/or project name when you will create the project. Lab Exercises 1. Create a reusable JavaFX LineChart component 2. Get use to and test GlassFish's REST monitoring API 3. Call GlassFish's Monitoring API from JavaFX 4. Add polling to your JavaFX application Please feel free to seek assistance from the instructor or Oracle Demo staff at any point during the lab. Page 2 of 49

  3. Exercise 1: Create a reusable JavaFX line chart component This exercise provides the instructions to build our first JavaFX component. We will create an animated line chart component, i.e. a component that will display a line graph, with a single line. When an application will add values to the graph, the line will move to the left side to display the new value and discard the oldest values, creating the appearance of an animation. Background Information Creating a custom JavaFX component. Creating a custom component for JavaFX is a three step process : • Create a class that will extend javafx.scene.CustomNode • Override the init { } block to create your own scene graph and included into the children property of the CustomNode • Add whatever public properties and functions that make sense for your component. This will be the interface clients can use to interact with your component. CustomNode class has a special property named children . It is an array of javafx.scene.Node objects. Laying out your own components is done through adding your Node to the children array. This is usually done in the init block of your CustomNode The interface that clients will use to create and communicate with your custom component is made of properties, i.e. values that you can set or read. And, functions that you can call or that are called back for notifications. A well designed component will only provide public access properties and function as interface. The interface must be designed as the contract between your component and its clients. This exercise is divided in two parts : 1. The first part gives instructions to create an Animated Line Chart component 2. The second part explains how to create a test application to actually display the Animated Line Chart component created during step #1 The video above shows the end result at the end of this exercise. Steps to Follow Step 1.1: Create the Project 1. If NetBeans is not already running, start it. 2. When NetBeans has started, select "New Project" from the "File" menu. Page 3 of 49

  4. 3. Browse to the JavaFX category, choose JavaFX Script Application and click Next 4. Type a name in Project Name: (such as Lab-S313674 for example), and click Finish: Page 4 of 49

  5. 5. NetBeans will create a project and an application skeleton. The main class is Main in package labs313674 Page 5 of 49

  6. Tip - How to reuse our source code ? The full source code, without line number for easy copy/paste, is provided in <lab-root>/solutions directory 1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 6 package lab_s313674; 7 8 import javafx.stage.Stage; 9 import javafx.scene.Scene; 10 import javafx.scene.text.Text; 11 import javafx.scene.text.Font; 12 13 /** 14 * @author sst 15 */ 16 17 Stage { 18 title: "Application title" 19 scene: Scene { 20 width: 250 21 height: 80 22 content: [ 23 Text { 24 font : Font { 25 size : 16 26 } 27 x: 10 28 y: 30 29 content: "Application content" 30 } 31 ] 32 } 33 } Show Line Numbers in NetBeans Editor You can turn on the line numbers in NetBeans' editor, select the "View" menu and click on "Show Line Numbers" , as shown below. Page 6 of 49

  7. Let's have a look at your first JavaFX application ! • Line 8-11 are traditional package declarations and import statements, just like you are used to. • Line 17 is the declaration of the top-level Window, the Stage . Stage is a JavaFX class, with its attributes being defined between the braces. • title is a Stage attribute. • scene at line 19 will define the content of the Stage . Its is another class instance, a Scene , having its own set of attributes, between braces. • width , height are Scene attributes. • The content of the Scene is a single Text component (line 23), itself having a Font attribute (line 24) and a text content (line 29) To launch the application, select "Run Main Project" from NetBeans' "Run" menu (or click on the green right arrow in the NetBeans' toolbar) Tip Page 7 of 49

  8. Depending on the platform and the NetBeans version used : when running JavaFX for the first time after installing the NetBeans plugin, an "accept" window might be displayed before the application runs. Should this "accept" window appears, click on "Accept" to further proceed. The application should look like this : Step 1.2: Create a custom Animated Line Chart Component The second step from this Exercise 1 is to create a new class to define our Animated Line Chart Component. This class will offer a very simple interface to its clients and will be based on the JavaFX javafx.scene.chart.LineChart component. 1. Select the labs313674 package in your project, right click the mouse button and choose "New..." then "JavaFX Class" Name your class AnimatedLineChart and define its package. To do this you click on the 'Package' drop- down to have the list of available packages in your project. Page 8 of 49

  9. Click Finish and the class will be created. NetBeans should open an editor window that will let us edit the source code of the class. 2. Add the extends JavaFX keyword to inherit from javafx.scene.CustomNode public class AnimatedLineChart extends CustomNode { 3. Define the public interface properties of your component, these are the attributes any client class will use to customize this component : //public properties public var title : String; public var lineName : String; public var xMaxValue : Integer; public var yMaxValue : Integer; • title and linename will hold the graph's title and the line legend respectively • xMaxValue and ymaxValue will define the upper bound on the X and Y axis respectively Page 9 of 49

Recommend


More recommend