JavaFX – Creating Scatter Chart

JavaFX – Scatter Chart ”; Previous Next A scatterplot is a type of graph which uses values from two variables plotted in a Cartesian plane. It is usually used to find out the relationship between two variables. Following is a Scatter chart plotted between area and weight. Scatter Chart in JavaFX In JavaFX, a Scatter chart is represented by a class named ScatterChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a ScatterChart node in JavaFX. To generate an area chart in JavaFX, follow the steps given below. Step 1: Defining the Axis Define the X and Y axis of the area chart and set labels to them in start() method of Application class. In our example, X axis represents area and the Y axis represents weights. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining the x axis NumberAxis xAxis = new NumberAxis(0, 12, 3); xAxis.setLabel(“Area”); //Defining the y axis NumberAxis yAxis = new NumberAxis(0, 16, 4); yAxis.setLabel(“Weight”); } } Step 2: Creating the Scatter Chart Create a line chart by instantiating the class named ScatterChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. //Creating the Scatter chart ScatterChart<String, Number> scatterChart = new ScatterChart(xAxis, yAxis); Step 3: Preparing the Data Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows − //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.getData().add(new XYChart.Data(8, 12)); series.getData().add(new XYChart.Data(4, 5.5)); series.getData().add(new XYChart.Data(11, 14)); series.getData().add(new XYChart.Data(4, 5)); series.getData().add(new XYChart.Data(3, 3.5)); series.getData().add(new XYChart.Data(6.5, 7)); Step 4: Add Data to the Scatter Chart Add the data series prepared in the previous step to the scatter chart as follows − //Setting the data to scatter chart scatterChart.getData().addAll(series); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the ScatterChart (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(scatterChart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following table contains sample data plotted between area and weight. Area Weight 8 12 4 5.5 11 14 4 5 3 3.5 6.5 7 Following is a Java program which generates a scatter chart depicting the above data using JavaFX. Save this code in a file with the name ScatterChartExample.java. import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; public class ScatterChartExample extends Application { @Override public void start(Stage stage) { //Defining the axes NumberAxis xAxis = new NumberAxis(0, 12, 3); xAxis.setLabel(“Area”); NumberAxis yAxis = new NumberAxis(0, 16, 4); yAxis.setLabel(“Weight”); //Creating the Scatter chart ScatterChart<String, Number> scatterChart = new ScatterChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.getData().add(new XYChart.Data(8, 12)); series.getData().add(new XYChart.Data(4, 5.5)); series.getData().add(new XYChart.Data(11, 14)); series.getData().add(new XYChart.Data(4, 5)); series.getData().add(new XYChart.Data(3, 3.5)); series.getData().add(new XYChart.Data(6.5, 7)); //Setting the data to scatter chart scatterChart.getData().addAll(series); //Creating a Group object Group root = new Group(scatterChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Scatter Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls ScatterChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ScatterChartExample Output On executing, the above program generates a JavaFX window displaying a scatter chart as shown below. Example The following table illustrates the number of electronic items that were in sold per month. Item Number of Sales (Per Month) Laptop 176 TV 30 Mobile 540 Smart Watch 250 MacBook 60 Following is a Java program which generates a scatter chart depicting the above data using JavaFX. Save this code in a file with the name ScatterChartSales.java. import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.ScatterChart; import javafx.scene.chart.XYChart; public class ScatterChartExample extends Application { @Override public void start(Stage stage) { //Defining the axes CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel(“Items”); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Sales (per month)”); //Creating the Scatter chart ScatterChart scatterChart = new ScatterChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“Items sold per month”); series.getData().add(new XYChart.Data(“Laptop”, 176)); series.getData().add(new XYChart.Data(“TV”, 30)); series.getData().add(new XYChart.Data(“Mobile”, 540)); series.getData().add(new XYChart.Data(“Smart Watch”, 250)); series.getData().add(new XYChart.Data(“MacBook”, 60)); //Setting the data to scatter chart scatterChart.getData().addAll(series); //Creating a Group object Group root = new Group(scatterChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Scatter Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls ScatterChartSales.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ScatterChartSales Output On executing, the above program generates a JavaFX window displaying a scatter chart as shown below. Print Page Previous Next Advertisements ”;

JavaFX – BorderPane Layout

JavaFX – BorderPane Layout ”; Previous Next BorderPane Layout in JavaFX The BorderPane is a layout control that arranges all the UI components of a JavaFX application into five distinc regions namely Top, Left, Right, Bottom and Center positions. The BorderPane layout pane is represented by a class named BorderPane of the javafx.scene.layout package. Instantiating this class will create a BorderPane layout. Constructors of this class are as follows − BorderPane() − It is the default constructor that creates an empty BorderPane. BorderPane(Node centerNode) − It constructs a new BorderPane layout and positions the node in the center. BorderPane(Node center, Node top, Node right, Node bottom, Node left) − This parameterized constructor of BorderPane class used to create a new BorderPane layout with the specified nodes. The BorderPane class contains five properties, which include − bottom − This property is of Node type and it represents the node placed at the bottom of the BorderPane. You can set value to this property using the setter method setBottom(). center − This property is of Node type and it represents the node placed at the center of the BorderPane. You can set value to this property using the setter method setCenter(). left − This property is of Node type and it represents the node placed at the left of the BorderPane. You can set value to this property using the setter method setLeft(). right − This property is of Node type and it represents the node placed at the right of the BorderPane. You can set value to this property using the setter method setRight(). top − This property is of Node type and it represents the node placed at the top of the BorderPane. You can set value to this property using the setter method setTop(). The figure below shows how JavaFX nodes are arranged in a BorderPane layout − In addition to the above mentioned properties and constructors, the BorderPane class also provides the following method − setAlignment() − This method is used to set the alignment of the nodes belonging to this pane. This method accepts a node and a priority value. Example The following program is an example of the BorderPane layout. In this, we are inserting a five text fields in the Top, Bottom, Right, Left and Center positions. Save this code in a file with the name BorderPaneExample.java. import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class BorderPaneExample extends Application { @Override public void start(Stage stage) { //Instantiating the BorderPane class BorderPane bPane = new BorderPane(); //Setting the top, bottom, center, right and left nodes to the pane bPane.setTop(new TextField(“Top”)); bPane.setBottom(new TextField(“Bottom”)); bPane.setLeft(new TextField(“Left”)); bPane.setRight(new TextField(“Right”)); bPane.setCenter(new TextField(“Center”)); //Creating a scene object Scene scene = new Scene(bPane, 400, 300); //Setting title to the Stage stage.setTitle(“BorderPane in JavaFX”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]) { launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BorderPaneExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BorderPaneExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Creating Stacked Bar Chart

JavaFX – Stacked Bar Chart ”; Previous Next StackedBarChart is a variation of a BarChart, which plots bars indicating data values for a category. The bars can be vertical or horizontal depending on which axis is the category axis. The bar for each series is stacked on top of the previous series. The following is a Stacked Bar Chart, which depicts the population growth. Stacked Bar Chart in JavaFX In JavaFX, a Stacked Bar Chart is represented by a class named StackedBarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a StackedBarChart node in JavaFX. To generate a Stacked Bar Chart in JavaFX, follow the steps given below. Step 1: Defining the Axis Define the X and Y axis of the stacked bar chart and set labels to them. In our example, X axis represents the continents and the y axis represents the population in millions. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining the x axis CategoryAxis xAxis = new CategoryAxis(); xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList (“Africa”, “America”, “Asia”, “Europe”, “Oceania”))); xAxis.setLabel(“category”); //Defining the y axis NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Population (In millions)”); } } Step 2: Creating the Stacked Bar Chart Create a line chart by instantiating the class named StackedBarChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. //Creating the Bar chart StackedBarChart<String, Number> stackedBarChart = new StackedBarChart<>(xAxis, yAxis); stackedBarChart.setTitle(“Historic World Population by Region”); Step 3: Preparing the Data Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows − //Prepare XYChart.Series objects by setting data XYChart.Series<String, Number> series1 = new XYChart.Series<>(); series1.setName(“1800”); series1.getData().add(new XYChart.Data<>(“Africa”, 107)); series1.getData().add(new XYChart.Data<>(“America”, 31)); series1.getData().add(new XYChart.Data<>(“Asia”, 635)); series1.getData().add(new XYChart.Data<>(“Europe”, 203)); series1.getData().add(new XYChart.Data<>(“Oceania”, 2)); XYChart.Series<String, Number> series2 = new XYChart.Series<>(); series2.setName(“1900”); series2.getData().add(new XYChart.Data<>(“Africa”, 133)); series2.getData().add(new XYChart.Data<>(“America”, 156)); series2.getData().add(new XYChart.Data<>(“Asia”, 947)); series2.getData().add(new XYChart.Data<>(“Europe”, 408)); series1.getData().add(new XYChart.Data<>(“Oceania”, 6)); XYChart.Series<String, Number> series3 = new XYChart.Series<>(); series3.setName(“2008”); series3.getData().add(new XYChart.Data<>(“Africa”, 973)); series3.getData().add(new XYChart.Data<>(“America”, 914)); series3.getData().add(new XYChart.Data<>(“Asia”, 4054)); series3.getData().add(new XYChart.Data<>(“Europe”, 732)); series1.getData().add(new XYChart.Data<>(“Oceania”, 34)); Step 4: Add Data to the Stacked Bar Chart Add the data series prepared in the previous step to the bar chart as follows − //Setting the data to bar chart stackedBarChart.getData().addAll(series1, series2, series3); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the StackedBarChart (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(stackedBarChart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following table lists out the population in various continents in the years 1800, 1900 and 2008. Africa America Asia Europe Oceania 1800 107 31 635 203 2 1900 133 156 947 408 6 2008 973 914 4054 732 34 Following is a Java program that generates a stacked bar chart depicting the above data, using JavaFX. Save this code in a file with the name StackedBarChartExample.java. import java.util.Arrays; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.StackedBarChart; import javafx.scene.chart.XYChart; public class StackedBarChartExample extends Application { @Override public void start(Stage stage) { //Defining the axes CategoryAxis xAxis = new CategoryAxis(); xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList (“Africa”, “America”, “Asia”, “Europe”, “Oceania”))); xAxis.setLabel(“category”); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Population (In millions)”); //Creating the Bar chart StackedBarChart<String, Number> stackedBarChart = new StackedBarChart<>(xAxis, yAxis); stackedBarChart.setTitle(“Historic World Population by Region”); //Prepare XYChart.Series objects by setting data XYChart.Series<String, Number> series1 = new XYChart.Series<>(); series1.setName(“1800”); series1.getData().add(new XYChart.Data<>(“Africa”, 107)); series1.getData().add(new XYChart.Data<>(“America”, 31)); series1.getData().add(new XYChart.Data<>(“Asia”, 635)); series1.getData().add(new XYChart.Data<>(“Europe”, 203)); series1.getData().add(new XYChart.Data<>(“Oceania”, 2)); XYChart.Series<String, Number> series2 = new XYChart.Series<>(); series2.setName(“1900”); series2.getData().add(new XYChart.Data<>(“Africa”, 133)); series2.getData().add(new XYChart.Data<>(“America”, 156)); series2.getData().add(new XYChart.Data<>(“Asia”, 947)); series2.getData().add(new XYChart.Data<>(“Europe”, 408)); series1.getData().add(new XYChart.Data<>(“Oceania”, 6)); XYChart.Series<String, Number> series3 = new XYChart.Series<>(); series3.setName(“2008”); series3.getData().add(new XYChart.Data<>(“Africa”, 973)); series3.getData().add(new XYChart.Data<>(“America”, 914)); series3.getData().add(new XYChart.Data<>(“Asia”, 4054)); series3.getData().add(new XYChart.Data<>(“Europe”, 732)); series1.getData().add(new XYChart.Data<>(“Oceania”, 34)); //Setting the data to bar chart stackedBarChart.getData().addAll(series1, series2, series3); //Creating a Group object Group root = new Group(stackedBarChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“stackedBarChart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls StackedBarChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls StackedBarChartExample Output On executing, the above program generates a JavaFX window displaying an area chart as shown below. Example The following table depicts the data of employees laid off and recruited in an organization, in the first quarter of an year. Month Employees Laid Off Employees Recruited January 30 12 February 12 54 March 31 24 April 52 32 May 4 43 June 10 5 Following is a Java program that generates a stacked bar chart depicting the above data, using JavaFX. Save this code in a file with the name StackedBarChartEmployees.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.StackedBarChart; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class StackedBarChartEmployees extends Application { @Override public void start(Stage stage) { //Defining the X axis CategoryAxis xAxis = new CategoryAxis(); //defining the y Axis NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Number of Employees”); //Creating

JavaFX – Creating Line Chart

JavaFX – Line Chart ”; Previous Next A line chart or line graph displays information as a series of data points (markers) connected by straight line segments. A Line Chart shows how the data changes at equal time frequency. Following is a Line chart depicting the number of schools in different years. Line Chart in JavaFX In JavaFX, a line chart is represented by a class named LineChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a LineChart node in JavaFX. To generate a line chart in JavaFX, you should follow the steps given below. Step 1: Defining the axis Define the X and Y axis of the line chart and set labels to them, within the start() method of Application class. In our example, the X axis represent the years starting from 1960 to 2020 having major tick mark at every ten years. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining X axis NumberAxis xAxis = new NumberAxis(1960, 2020, 10); xAxis.setLabel(“Years”); //Defining y axis NumberAxis yAxis = new NumberAxis(0, 350, 50); yAxis.setLabel(“No.of schools”); } } Step 2: Creating the Line Chart Create a line chart by instantiating the class named LineChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. LineChart linechart = new LineChart(xAxis, yAxis); Step 3: Preparing the Data Instantiate the XYChart.Series class. Then add the data (a series of, x and y coordinates) to the Observable list of this class as follows − XYChart.Series series = new XYChart.Series(); series.setName(“No of schools in an year”); series.getData().add(new XYChart.Data(1970, 15)); series.getData().add(new XYChart.Data(1980, 30)); series.getData().add(new XYChart.Data(1990, 60)); series.getData().add(new XYChart.Data(2000, 120)); series.getData().add(new XYChart.Data(2013, 240)); series.getData().add(new XYChart.Data(2014, 300)); Step 4: Add Data to the Line Chart Add the data series prepared in the previous step to the line chart as follows − //Setting the data to Line chart linechart.getData().add(series); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the LineChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(linechart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following table depicts the number of schools that were in an area from the year 1970 to 2014. Year Number of Schools 1970 15 1980 30 1990 60 2000 120 2013 240 2014 300 Following is a Java program which generates a line chart, depicting the above data, using JavaFX. Save this code in a file with the name LineChartExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class LineChartExample extends Application { @Override public void start(Stage stage) { //Defining the x axis NumberAxis xAxis = new NumberAxis(1960, 2020, 10); xAxis.setLabel(“Years”); //Defining the y axis NumberAxis yAxis = new NumberAxis (0, 350, 50); yAxis.setLabel(“No.of schools”); //Creating the line chart LineChart linechart = new LineChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“No of schools in an year”); series.getData().add(new XYChart.Data(1970, 15)); series.getData().add(new XYChart.Data(1980, 30)); series.getData().add(new XYChart.Data(1990, 60)); series.getData().add(new XYChart.Data(2000, 120)); series.getData().add(new XYChart.Data(2013, 240)); series.getData().add(new XYChart.Data(2014, 300)); //Setting the data to Line chart linechart.getData().add(series); //Creating a Group object Group root = new Group(linechart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Line Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls LineChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls LineChartExample Output On executing, the above program generates a JavaFX window displaying a line chart as shown below. Example The following table illustrates the number of electronic items that were in sold per month. Item Number of Sales (Per Month) Laptop 176 TV 30 Mobile 540 Smart Watch 250 MacBook 60 In another example, a line chart depicting the above data, is generated using JavaFX. Save this code in a file with the name LineChartItems.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.XYChart; public class LineChartItems extends Application { @Override public void start(Stage stage) { //Defining the x axis CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel(“Items”); //Defining the y axis NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Sales (per month)”); //Creating the line chart LineChart linechart = new LineChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“Items sold per month”); series.getData().add(new XYChart.Data(“Laptop”, 176)); series.getData().add(new XYChart.Data(“TV”, 30)); series.getData().add(new XYChart.Data(“Mobile”, 540)); series.getData().add(new XYChart.Data(“Smart Watch”, 250)); series.getData().add(new XYChart.Data(“MacBook”, 60)); //Creating a scene object Scene scene = new Scene(linechart, 600, 400); //Setting the data to Line chart linechart.getData().add(series); //Setting title to the Stage stage.setTitle(“Line Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls LineChartItems.java java –module-path %PATH_TO_FX% –add-modules javafx.controls LineChartItems Output On executing, the above program generates a JavaFX window displaying a line chart as shown below.

JavaFX – Creating Area Chart

JavaFX – Area Chart ”; Previous Next Area charts are used to draw area based charts. It plots the area between the given series of points and the axis. In general, this chart is used to compare two quantities. Following is an Area chart depicting the number of fruits consumed by two people in a week. Area Chart in JavaFX In JavaFX, an Area chart is represented by a class named AreaChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create an AreaChart node in JavaFX. To generate an area chart in JavaFX, follow the steps given below. Step 1: Defining the Axis Define the X and Y axis of the area chart and set labels to them. In our example, X axis represents the days in a week and the y axis represents the units of fruits consumed. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining the X axis CategoryAxis xAxis = new CategoryAxis(); //Defining the y Axis NumberAxis yAxis = new NumberAxis(0, 15, 2.5); yAxis.setLabel(“Fruit units”); } } Step 2: Creating the Area Chart Create a line chart by instantiating the class named AreaChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. //Creating the Area chart AreaChart<String, Number> areaChart = new AreaChart(xAxis, yAxis); areaChart.setTitle(“Average fruit consumption during one week”); Step 3: Preparing the data Instantiate the XYChart.Series class. Then add the data (a series of, x and y coordinates) to the Observable list of this class as follows − //Prepare XYChart.Series objects by setting data XYChart.Series series1 = new XYChart.Series(); series1.setName(“John”); series1.getData().add(new XYChart.Data(“Monday”, 3)); series1.getData().add(new XYChart.Data(“Tuesday”, 4)); series1.getData().add(new XYChart.Data(“Wednesday”, 3)); series1.getData().add(new XYChart.Data(“Thursday”, 5)); series1.getData().add(new XYChart.Data(“Friday”, 4)); series1.getData().add(new XYChart.Data(“Saturday”, 10)); series1.getData().add(new XYChart.Data(“Sunday”, 12)); XYChart.Series series2 = new XYChart.Series(); series2.setName(“Jane”); series2.getData().add(new XYChart.Data(“Monday”, 1)); series2.getData().add(new XYChart.Data(“Tuesday”, 3)); series2.getData().add(new XYChart.Data(“Wednesday”, 4)); series2.getData().add(new XYChart.Data(“Thursday”, 3)); series2.getData().add(new XYChart.Data(“Friday”, 3)); series2.getData().add(new XYChart.Data(“Saturday”, 5)); series2.getData().add(new XYChart.Data(“Sunday”, 4)); Step 4: Add Data to the Area Chart Add the data series prepared in the previous step to the area chart as follows − //Setting the XYChart.Series objects to area chart areaChart.getData().addAll(series1,series2); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group, which belongs to the package javafx.scene. Pass the AreaChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(areaChart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following table depicts the number of fruits consumed by John and Jane in a week. Day of the Week Fruits consumed by John Fruits consumed by Jane Monday 3 1 Tuesday 4 3 Wednesday 3 4 Thursday 5 3 Friday 4 3 Saturday 10 5 Sunday 12 4 Following is a Java program which generates an area chart, depicting the above data using JavaFX. Save this code in a file with the name AreaChartExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.AreaChart; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class AreaChartExample extends Application { @Override public void start(Stage stage) { //Defining the X axis CategoryAxis xAxis = new CategoryAxis(); //defining the y Axis NumberAxis yAxis = new NumberAxis(0, 15, 2.5); yAxis.setLabel(“Fruit units”); //Creating the Area chart AreaChart<String, Number> areaChart = new AreaChart(xAxis, yAxis); areaChart.setTitle(“Average fruit consumption during one week”); //Prepare XYChart.Series objects by setting data XYChart.Series series1 = new XYChart.Series(); series1.setName(“John”); series1.getData().add(new XYChart.Data(“Monday”, 3)); series1.getData().add(new XYChart.Data(“Tuesday”, 4)); series1.getData().add(new XYChart.Data(“Wednesday”, 3)); series1.getData().add(new XYChart.Data(“Thursday”, 5)); series1.getData().add(new XYChart.Data(“Friday”, 4)); series1.getData().add(new XYChart.Data(“Saturday”, 10)); series1.getData().add(new XYChart.Data(“Sunday”, 12)); XYChart.Series series2 = new XYChart.Series(); series2.setName(“Jane”); series2.getData().add(new XYChart.Data(“Monday”, 1)); series2.getData().add(new XYChart.Data(“Tuesday”, 3)); series2.getData().add(new XYChart.Data(“Wednesday”, 4)); series2.getData().add(new XYChart.Data(“Thursday”, 3)); series2.getData().add(new XYChart.Data(“Friday”, 3)); series2.getData().add(new XYChart.Data(“Saturday”, 5)); series2.getData().add(new XYChart.Data(“Sunday”, 4)); //Setting the XYChart.Series objects to area chart areaChart.getData().addAll(series1,series2); //Creating a Group object Group root = new Group(areaChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Area Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls AreaChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls AreaChartExample Output On executing, the above program generates a JavaFX window displaying an area chart as shown below. Example Let us see another example where we are trying to depict the population increase in two different cities through the years 2017 – 2022. Years Population in City A Population in City B 2017 3000 1678 2018 4573 2374 2019 5753 4124 2020 6476 5963 2021 7340 7570 2022 8301 8500 Save this code in a file with the name AreaChartPopulation.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.AreaChart; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class AreaChartPopulation extends Application { @Override public void start(Stage stage) { //Defining the X axis NumberAxis xAxis = new NumberAxis(2017, 2022, 1); xAxis.setLabel(“Years”); //defining the y Axis NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“Population”); //Creating the Area chart AreaChart<String, Number> areaChart = new AreaChart(xAxis, yAxis); areaChart.setTitle(“Population in Two Cities Over Years”); //Prepare XYChart.Series objects by setting data XYChart.Series series1 = new XYChart.Series(); series1.setName(“City A”); series1.getData().add(new XYChart.Data(2017, 3000)); series1.getData().add(new XYChart.Data(2018, 4573)); series1.getData().add(new XYChart.Data(2019, 5753)); series1.getData().add(new XYChart.Data(2020, 6476)); series1.getData().add(new XYChart.Data(2021, 7340)); series1.getData().add(new XYChart.Data(2022, 8301));

JavaFX – Creating Bubble Chart

JavaFX – Bubble Chart ”; Previous Next A bubble chart is used to plant three-dimensional data; the third dimension will be represented by the size (radius) of the bubble. The following is a Bubble chart depicting the work done. Bubble Chart in JavaFX In JavaFX, a Bubble chart is represented by a class named BubbleChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create an BubbleChart node in JavaFX. To generate a bubble chart in JavaFX, follow the steps given below. Step 1: Defining the Axis Define the X and Y axis of the bubble chart and set labels to them. In our example, X axis represents the age, Y axis represents the weight. While, the radius of the bubble represents the work done. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining the X axis NumberAxis xAxis = new NumberAxis(0, 100, 10); xAxis.setLabel(“Age”); //Defining Y axis NumberAxis yAxis = new NumberAxis(20, 100, 10); yAxis.setLabel(“Weight”); } } Step 2: Creating the Bubble Chart Create a line chart by instantiating the class named BubbleChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. //Creating the Bubble chart BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis); Step 3: Preparing the Data Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows − //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“work”); series.getData().add(new XYChart.Data(10,30,4)); series.getData().add(new XYChart.Data(25,40,5)); series.getData().add(new XYChart.Data(40,50,9)); series.getData().add(new XYChart.Data(55,60,7)); series.getData().add(new XYChart.Data(70,70,9)); series.getData().add(new XYChart.Data(85,80,6)); Step 4: Add Data to the Bubble Chart Add the data series prepared in the previous step to the area chart as follows − //Setting the data to bar chart bubbleChart.getData().add(series); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the BubbleChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(bubbleChart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Let us consider different persons along with their age, weight and work capacities. The work capacity can be treated as the number of hours that is plotted as bubbles in the chart. WEIGHT AGE 30 40 50 60 70 80 10 4 WORK 25 5 40 6 55 8 70 9 85 15 Following is a Java program which generates a bubble chart, depicting the above data using JavaFX. Save this code in a file with the name BubbleChartExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.BubbleChart; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class BubbleChartExample extends Application { @Override public void start(Stage stage) { //Defining the axes NumberAxis xAxis = new NumberAxis(0, 100, 10); xAxis.setLabel(“Age”); NumberAxis yAxis = new NumberAxis(20, 100, 10); yAxis.setLabel(“Weight”); //Creating the Bubble chart BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“work”); series.getData().add(new XYChart.Data(10,30,4)); series.getData().add(new XYChart.Data(25,40,5)); series.getData().add(new XYChart.Data(40,50,9)); series.getData().add(new XYChart.Data(55,60,7)); series.getData().add(new XYChart.Data(70,70,9)); series.getData().add(new XYChart.Data(85,80,6)); //Setting the data to bar chart bubbleChart.getData().add(series); //Creating a Group object Group root = new Group(bubbleChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Bubble Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BubbleChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BubbleChartExample Output On executing, the above program generates a JavaFX window displaying a bubble chart as shown below. Example The following table depicts the number of schools that were in an area from the year 1970 to 2014. Year Number of Schools 1970 15 1980 30 1990 60 2000 120 2013 240 2014 300 Following is a Java program which generates a bubble chart, depicting the above data using JavaFX. Save this code in a file with the name BubbleChartSchools.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.BubbleChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class BubbleChartSchools extends Application { @Override public void start(Stage stage) { //Defining the x axis NumberAxis xAxis = new NumberAxis(1960, 2020, 10); xAxis.setLabel(“Years”); //Defining the y axis NumberAxis yAxis = new NumberAxis(20, 320, 20); yAxis.setLabel(“No.of schools”); //Creating the Bubble chart BubbleChart bubblechart = new BubbleChart(xAxis, yAxis); //Prepare XYChart.Series objects by setting data XYChart.Series series = new XYChart.Series(); series.setName(“No of schools in an year”); // Add a third coordinate representing the radius of bubble series.getData().add(new XYChart.Data(1970, 25, 1)); series.getData().add(new XYChart.Data(1980, 30, 2)); series.getData().add(new XYChart.Data(1990, 60, 3)); series.getData().add(new XYChart.Data(2000, 120, 4)); series.getData().add(new XYChart.Data(2013, 240, 5)); series.getData().add(new XYChart.Data(2014, 300, 6)); //Setting the data to bubble chart bubblechart.getData().add(series); //Creating a Group object Group root = new Group(bubblechart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Bubble Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BubbleChartSchools.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BubbleChartSchools Output On executing, the above program

JavaFX – Creating Pie Chart

JavaFX – Pie Chart ”; Previous Next A pie-chart is a representation of values as slices of a circle with different colors. These slices are labeled and the values corresponding to each slice is represented in the chart. Following is a Pie Chart depicting the mobile sales of various companies at an instance. Pie Chart in JavaFX In JavaFX, a pie chart is represented by a class named PieChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a PieChart node in JavaFX. This class has 5 properties which are as follows − clockwise − This is a Boolean Operator; on setting this operator true, the data slices in the pie charts will be arranged clockwise starting from the start angle of the pie chart. data − This represents an ObservableList object, which holds the data of the pie chart. labelLineLength − An integer operator representing the length of the lines connecting the labels and the slices of the pie chart. labelsVisible − This is a Boolean Operator; on setting this operator true, the labels for the pie charts will be drawn. By default, this operator is set to be true. startAngle − This is a double type operator, which represents the angle to start the first pie slice at. To generate a pie chart, Prepare an ObservableList object. After preparing the ObservableList object, pass it as an argument to the constructor of the class PieChart; Or, by using the method named setData(). Steps to Generate Pie Chart To generate a PieChart in JavaFX, follow the steps given below. Step 1: Creating a Class Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class as follows. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { } } Step 2: Preparing the ObservableList Object Prepare an object of the interface ObservableList object by passing the data of the pie chart as shown below − ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data(“Iphone 5S”, 13), new PieChart.Data(“Samsung Grand”, 25), new PieChart.Data(“MOTO G”, 10), new PieChart.Data(“Nokia Lumia”, 22)); Step 3: Creating a PieChart Object Create a PieChart by passing the ObservableList object as shown below. //Creating a Pie chart PieChart pieChart = new PieChart(pieChartData); Step 4: Setting the Title of the Pie Chart Set the title of the Pie Chart using the setTitle() method of the class PieChart. This belongs to the package javafx.scene.chart − //Setting the title of the Pie chart pieChart.setTitle(“Mobile Sales”); Step 5: Setting the Slices Clockwise Set the slices of the Pie Charts clockwise. This is done by passing Boolean value true to the setClockwise() method of the class PieChart. This belongs to the package javafx.scene.chart − //setting the direction to arrange the data pieChart.setClockwise(true); Step 6: Set the Length of the Label Line Set the length of the label line using the setLabelLineLength() method of the class PieChart which belongs to the package javafx.scene.chart, as follows − //Setting the length of the label line pieChart.setLabelLineLength(50); Step 7: Set the Labels Visible Set the labels of the pie chart to visible by passing the Boolean value true to the method setLabelsVisible() of the class PieChart. This belongs to the package javafx.scene.chart − //Setting the labels of the pie chart visible pieChart.setLabelsVisible(true); Step 8: Set the Start Angle of the Pie Chart Set the Start angle of the pie chart using the setStartAngle() method of the class PieChart. This belongs to the package javafx.scene.chart − //Setting the start angle of the pie chart pieChart.setStartAngle(180); Step 9: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the PieChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(piechart); Step 10: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The table given below depicts mobile sale with the help of a pie chart. The following table has a list of different mobile brands and their sale (units per day). S.No Mobile Brands Sales (Units per day) 1 Iphone 5S 20 2 Samsung Grand 20 3 MOTO G 40 4 Nokia Lumia 10 Following is a Java program which generates a pie chart, depicting the above data using JavaFX. Save this code in a file with the name PieChartExample.java. import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.chart.PieChart; public class PieChartExample extends Application { @Override public void start(Stage stage) { //Preparing ObservbleList object ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data(“Iphone 5S”, 13), new PieChart.Data(“Samsung Grand”, 25), new PieChart.Data(“MOTO G”, 10), new PieChart.Data(“Nokia Lumia”, 22)); //Creating a Pie chart PieChart pieChart = new PieChart(pieChartData); //Setting the title of the Pie chart pieChart.setTitle(“Mobile Sales”); //setting the direction to arrange the data pieChart.setClockwise(true); //Setting the length of the label line pieChart.setLabelLineLength(50); //Setting the labels of the pie chart visible pieChart.setLabelsVisible(true); //Setting the start angle of the pie chart pieChart.setStartAngle(180); //Creating a Group object Group root = new Group(pieChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Pie chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and

JavaFX – Creating Bar Chart

JavaFX – Bar Chart ”; Previous Next A bar chart is used to represent grouped data using rectangular bars. The length of these bars depicts the values. The bars in the bar chart can be plotted vertically or horizontally. Following is a bar chart, comparing various car brands. Bar Chart in JavaFX In JavaFX, a Bar chart is represented by a class named BarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create an BarChart node in JavaFX. To generate a bar chart in JavaFX, follow the steps given below. Step 1: Defining the Axis In the start() method of Application class, define the X and Y axis of the bar chart and set labels to them. In our example, X axis represents the category of comparison and the y axis represents the score. public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception { //Defining the x axis CategoryAxis xAxis = new CategoryAxis(); xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList( “Speed”, “User rating”, “Milage”, “Safety”))); xAxis.setLabel(“category”); //Defining the y axis NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“score”); } } Step 2: Creating the Bar Chart Create a line chart by instantiating the class named BarChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step. //Creating the Bar chart BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis); barChart.setTitle(“Comparison between various cars”); Step 3: Preparing the Data Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows − //Prepare XYChart.Series objects by setting data XYChart.Series<String, Number> series1 = new XYChart.Series<>(); series1.setName(“Fiat”); series1.getData().add(new XYChart.Data<>(“Speed”, 1.0)); series1.getData().add(new XYChart.Data<>(“User rating”, 3.0)); series1.getData().add(new XYChart.Data<>(“Milage”, 5.0)); series1.getData().add(new XYChart.Data<>(“Safety”, 5.0)); XYChart.Series<String, Number> series2 = new XYChart.Series<>(); series2.setName(“Audi”); series2.getData().add(new XYChart.Data<>(“Speed”, 5.0)); series2.getData().add(new XYChart.Data<>(“User rating”, 6.0)); series2.getData().add(new XYChart.Data<>(“Milage”, 10.0)); series2.getData().add(new XYChart.Data<>(“Safety”, 4.0)); XYChart.Series<String, Number> series3 = new XYChart.Series<>(); series3.setName(“Ford”); series3.getData().add(new XYChart.Data<>(“Speed”, 4.0)); series3.getData().add(new XYChart.Data<>(“User rating”, 2.0)); series3.getData().add(new XYChart.Data<>(“Milage”, 3.0)); series3.getData().add(new XYChart.Data<>(“Safety”, 6.0)); Step 4: Add Data to the Bar Chart Add the data series prepared in the previous step to the bar chart as follows − //Setting the data to bar chart barChart.getData().addAll(series1, series2, series3); Step 5: Creating a Group Object In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene. Pass the BarChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows − Group root = new Group(barChart); Step 6: Launching Application Lastly, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example The following example depicts various car statistics with the help of a bar chart. Following is a list of car brands along with their different characteristics, which we will show using a bar chart − Car Speed User Rating Millage Safety Fiat 1.0 3.0 5.0 5.0 Audi 5.0 6.0 10.0 4.0 Ford 4.0 2.0 3.0 6.0 Following is a Java program which generates a bar chart, depicting the above data using JavaFX. Save this code in a file with the name BarChartExample.java. import java.util.Arrays; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.BarChart; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class BarChartExample extends Application { @Override public void start(Stage stage) { //Defining the axes CategoryAxis xAxis = new CategoryAxis(); xAxis.setCategories(FXCollections.<String> observableArrayList(Arrays.asList(“Speed”, “User rating”, “Milage”, “Safety”))); xAxis.setLabel(“category”); NumberAxis yAxis = new NumberAxis(); yAxis.setLabel(“score”); //Creating the Bar chart BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis); barChart.setTitle(“Comparison between various cars”); //Prepare XYChart.Series objects by setting data XYChart.Series<String, Number> series1 = new XYChart.Series<>(); series1.setName(“Fiat”); series1.getData().add(new XYChart.Data<>(“Speed”, 1.0)); series1.getData().add(new XYChart.Data<>(“User rating”, 3.0)); series1.getData().add(new XYChart.Data<>(“Milage”, 5.0)); series1.getData().add(new XYChart.Data<>(“Safety”, 5.0)); XYChart.Series<String, Number> series2 = new XYChart.Series<>(); series2.setName(“Audi”); series2.getData().add(new XYChart.Data<>(“Speed”, 5.0)); series2.getData().add(new XYChart.Data<>(“User rating”, 6.0)); series2.getData().add(new XYChart.Data<>(“Milage”, 10.0)); series2.getData().add(new XYChart.Data<>(“Safety”, 4.0)); XYChart.Series<String, Number> series3 = new XYChart.Series<>(); series3.setName(“Ford”); series3.getData().add(new XYChart.Data<>(“Speed”, 4.0)); series3.getData().add(new XYChart.Data<>(“User rating”, 2.0)); series3.getData().add(new XYChart.Data<>(“Milage”, 3.0)); series3.getData().add(new XYChart.Data<>(“Safety”, 6.0)); //Setting the data to bar chart barChart.getData().addAll(series1, series2, series3); //Creating a Group object Group root = new Group(barChart); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle(“Bar Chart”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BarChartExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BarChartExample Output On executing, the above program generates a JavaFX window displaying an area chart as shown below. Example The following table depicts the number of fruits consumed by John and Jane in a week. Day of the Week Fruits consumed by John Fruits consumed by Jane Monday 3 1 Tuesday 4 3 Wednesday 3 4 Thursday 5 3 Friday 4 3 Saturday 10 5 Sunday 12 4 Following is a Java program which generates an bar chart, depicting the above data using JavaFX. Save this code in a file with the name BarChartFruits.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.BarChart; import javafx.scene.chart.CategoryAxis; import javafx.stage.Stage; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; public class BarChartFruits extends Application { @Override public void start(Stage stage) { //Defining the X axis CategoryAxis xAxis = new CategoryAxis(); //defining the y Axis NumberAxis yAxis = new NumberAxis(0, 15, 2.5); yAxis.setLabel(“Fruit units”); //Creating the Area chart BarChart<String, Number> barChart = new BarChart(xAxis, yAxis); barChart.setTitle(“Average fruit consumption during

JavaFX – TableView

JavaFX – TableView ”; Previous Next The TableView is a graphical user interface component used to display data in a tabular form. Similar to a typical table, TableView consists of columns, rows, and cells, each of which can hold any type of data. In general, a table is represented as shown in the following figure − TableView in JavaFX In JavaFX, the table view is represented by the TableView class. This class is a part of the package named javafx.scene.control. By instantiating this class, we can create a TableView node in JavaFX. It has two constructors namely − TableView() − It is the default constructor used to create a table view without any predefined data. TableView(ObservableList items) − It is the parameterized constructor of TableView class which accepts a list of items and creates a new table view with the specified items. Steps to create a TableView in JavaFX Follow the steps given below to create a table view in JavaFX. Step 1: Create a Class to represent Data within the Table First, we need to create a class that represents the data to display within the TableView. This class should have properties that correspond to the columns of the table. Here, we will create a class named movie with properties title, actor, price and rating by using the below code blocks − // The data model class public static class Movie { private final SimpleStringProperty title; private final SimpleStringProperty actor; private final SimpleDoubleProperty price; private final SimpleIntegerProperty rating; Step 2: Create the Columns for the TableView Create TableColumn objects and set their cell value using the setCellValueFactory() method. A cell value factory is a function that tells the column how to extract the data from the above declared data model class. The following code block shows how to create columns − // Creating columns for the TableView TableColumn<Movie, String> titleColumn = new TableColumn<>(“Movie Name”); titleColumn.setCellValueFactory(cellData -> cellData.getValue().titleProperty()); TableColumn<Movie, String> actorColumn = new TableColumn<>(“Actor”); actorColumn.setCellValueFactory(cellData -> cellData.getValue().authorProperty()); TableColumn<Movie, Number> priceColumn = new TableColumn<>(“Price”); priceColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty()); TableColumn<Movie, Number> ratingColumn = new TableColumn<>(“IMDB Rating”); ratingColumn.setCellValueFactory(cellData -> cellData.getValue().ratingProperty()); Step 3: Instantiate TableView class Instantiate the TableView class of package javafx.scene.control without passing any parameter value to its constructor and add all the columns using the following code blocks − // Creating a TableView TableView<Movie> tableView = new TableView<>(); // Adding the columns to the TableView tableView.getColumns().addAll(titleColumn, actorColumn, priceColumn, ratingColumn); Step 4: Launching Application Once the table view is created and all the data are added, follow the given steps below to launch the application properly − Firstly, instantiate the class named BorderPane by passing the TableView object as a parameter value to its constructor. Then, instantiate the class named Scene by passing the BorderPane object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is the program that will create a TableView using JavaFX. Save this code in a file with the name JavafxTableview.java. import javafx.application.Application; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class JavafxTableview extends Application { // The data model class public static class Movie { private final SimpleStringProperty title; private final SimpleStringProperty actor; private final SimpleDoubleProperty price; private final SimpleIntegerProperty rating; // constructor public Movie(String title, String actor, double price, int rating) { this.title = new SimpleStringProperty(title); this.actor = new SimpleStringProperty(actor); this.price = new SimpleDoubleProperty(price); this.rating = new SimpleIntegerProperty(rating); } // getters and setters to access the data public SimpleStringProperty titleProperty() { return title; } public SimpleStringProperty authorProperty() { return actor; } public SimpleDoubleProperty priceProperty() { return price; } public SimpleIntegerProperty ratingProperty() { return rating; } } // main method starts here @Override public void start(Stage stage) throws Exception { // adding some sample data ObservableList<Movie> movies = FXCollections.observableArrayList( new Movie(“The Batman”, “Robert Pattinson”, 299, 7), new Movie(“John Wick: Chapter 4”, “Keanu Reeves”, 199, 7), new Movie(“12th Fail”, “Vikrant Massey”, 199, 9), new Movie(“Money Heist”, “Alvaro Morte”, 499, 8), new Movie(“The Family Man”, “Manoj Bajpayee”, 399, 8) ); // Creating a TableView TableView<Movie> tableView = new TableView<>(); // Creating columns for the TableView TableColumn<Movie, String> titleColumn = new TableColumn<>(“Movie Name”); titleColumn.setCellValueFactory(cellData -> cellData.getValue().titleProperty()); TableColumn<Movie, String> actorColumn = new TableColumn<>(“Actor”); actorColumn.setCellValueFactory(cellData -> cellData.getValue().authorProperty()); TableColumn<Movie, Number> priceColumn = new TableColumn<>(“Price”); priceColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty()); TableColumn<Movie, Number> ratingColumn = new TableColumn<>(“IMDB Rating”); ratingColumn.setCellValueFactory(cellData -> cellData.getValue().ratingProperty()); // Adding the columns to the TableView tableView.getColumns().addAll(titleColumn, actorColumn, priceColumn, ratingColumn); // Set the items of the TableView tableView.setItems(movies); // Create a BorderPane and set the TableView as its center BorderPane root = new BorderPane(); root.setCenter(tableView); // Create a Scene and set it on the Stage Scene scene = new Scene(root, 400, 300); stage.setTitle(“Table View in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTableview.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTableview Output On executing, the above program generates a JavaFX window displaying a Tableview with a list of movies as shown below. Creating Nested columns of TableView Sometimes, we are required to display data of a single column in multiple sub-columns. This is common when a single entity has multiple attributes, for instance, a person can have multiple contact numbers or email accounts. In

JavaFX – TreeView

JavaFX – TreeView ”; Previous Next The TreeView is a graphical user interface component that displays a hierarchical structure of items. It consists of a root node and any number of child nodes. Primarily, the tree view is used for organizing data with hierarchy. It provides a better understanding of data and its relation with other components. In the below figure, we can see a tree view of components. TreeView in JavaFX In JavaFX, the treeview is represented by a class named TreeView. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a treeview in JavaFX. Constructors of the TreeView class are listed below − TreeView() − It is the default constructor that constructs an empty treeview. TreeView(TreeItem rootNode) − It creates a new treeview with the specified root node. How to create a TreeView in JavaFX? Follow the steps given below to create a tree view in JavaFX. Step 1: Create the root node of TreeView First, create containers for the lists of all items. In this case, root node is the container as it holds all the child nodes. To create the root node, we use the TreeItem class which is the part of javafx.scene.control package. Pass the name of root node to the constructor of this class as a parameter value. − // Creating the root node TreeItem<String> root = new TreeItem<>(“Smartphones”); Step 2: Create the child nodes of TreeView Again, use the TreeItem class to create child nodes. Once the child nodes are created, use the getChildren() method along with addAll() to add the required items to it. TreeItem<String> ios = new TreeItem<>(“iOS”); ios.getChildren().addAll( new TreeItem<>(“iPhone 15 Plus”), new TreeItem<>(“iPhone 14”) ); Step 3: Add the child nodes to root nodes To add the child nodes to the root node, pass the name of child nodes as a parameter value to the addAll() method as shown in the following code blocks − // Add the child nodes to the root node root.getChildren().addAll(android, ios); Step 4: Instantiate the TreeView class To create tree view, instantiate the TreeView class and pass the root node to its constructor as a parameter value as shown in the below code − // Create the TreeView and add the root node TreeView<String> treesV = new TreeView<>(root); Step 5: Launching Application Once the tree view is created, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the TreeView object as a parameter value to its constructor. We can also pass the dimensions of the application screen as optional parameters to this constructor. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is the program that will create a TreeView using JavaFX. Save this code in a file with the name JavafxTreeview.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.stage.Stage; public class JavafxTreeview extends Application { @Override public void start(Stage stage) throws Exception { // Creating the root node TreeItem<String> root = new TreeItem<>(“Smartphones”); root.setExpanded(true); // Creating its child nodes TreeItem<String> android = new TreeItem<>(“Android”); android.getChildren().addAll( new TreeItem<>(“Samsung Galaxy S23 Ultra”), new TreeItem<>(“Xiaomi Redmi Note 13 Pro”), new TreeItem<>(“OnePlus 11R”) ); TreeItem<String> ios = new TreeItem<>(“iOS”); ios.getChildren().addAll( new TreeItem<>(“iPhone 15 Plus”), new TreeItem<>(“iPhone 14”) ); // Add the child nodes to the root node root.getChildren().addAll(android, ios); // Create the TreeView and add the root node TreeView<String> treesV = new TreeView<>(root); // Create a scene and add the TreeView Scene scene = new Scene(treesV, 400, 300); stage.setTitle(“TreeView in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTreeview.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTreeview Output On executing, the above program generates a JavaFX window displaying a TreeView with a list of smartphones as shown below. Setting the mouse events for TreeView If we want to show which items are getting clicked by users, then, we can use setOnMouseClicked() method by passing a lambda expression as shown in the below JavaFX code. Save this code in a file with the name JavafxTreeview.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.stage.Stage; public class JavafxTreeview extends Application { @Override public void start(Stage stage) throws Exception { // Creating the root node TreeItem<String> root = new TreeItem<>(“Smartphones”); root.setExpanded(true); // Creating its child nodes TreeItem<String> android = new TreeItem<>(“Android”); android.getChildren().addAll( new TreeItem<>(“Samsung Galaxy S23 Ultra”), new TreeItem<>(“Xiaomi Redmi Note 13 Pro”), new TreeItem<>(“OnePlus 11R”) ); TreeItem<String> ios = new TreeItem<>(“iOS”); ios.getChildren().addAll( new TreeItem<>(“iPhone 15 Plus”), new TreeItem<>(“iPhone 14”) ); // Add the child nodes to the root node root.getChildren().addAll(android, ios); // Create the TreeView and add the root node TreeView<String> treesV = new TreeView<>(root); // Handle mouse clicks on the nodes treesV.setOnMouseClicked(event -> { // Get the selected item TreeItem<String> item = treesV.getSelectionModel().getSelectedItem(); if (item != null) { // Display the item text System.out.println(item.getValue()); } }); // Create a scene and add the TreeView Scene scene = new Scene(treesV, 400, 300); stage.setTitle(“TreeView in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTreeview.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxTreeview Output On executing the above code, it will