We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
JavaFX
Dr. S Sudheer Mangalampalli
Assistant Professor, Senior Grade1 School of Computer Science and Engineering VIT-AP University
July 6, 2022
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 1/9 JavaFX Application Structure
JavaFX application is divided hierarchically into three main components
known as Stage, Scene and nodes. We need to import javafx.application.Application class in every JavaFX application. This provides the following life cycle methods for JavaFX application. public void init() public abstract void start(Stage primaryStage) public void stop() to create a basic JavaFX application, we need to: Import javafx.application.Application into our code. Inherit Application into our class. Override start() method of Application class.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 2/9 Stage Stage in a JavaFX application is similar to the Frame in a Swing Application. It acts like a container for all the JavaFX objects. Primary Stage is created internally by the platform. Other stages can further be created by the application. The object of primary stage is passed to start method. We need to call show method on the primary stage object in order to show our primary stage.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 3/9 Scene- Scene actually holds all the physical contents (nodes) of a JavaFX application. Javafx.scene.Scene class provides all the methods to deal with a scene object. Creating scene is necessary in order to visualize the contents on the stage. At one instance, the scene object can only be added to one stage. In order to implement Scene in our JavaFX application, we must import javafx.scene package in our code. The Scene can be created by creating the Scene class object and passing the layout object into the Scene class constructor.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 4/9 Scene Graph- Scene Graph exists at the lowest level of the hierarchy. It can be seen as the collection of various nodes. A node is the element which is visualized on the stage. It can be any button, text box, layout, image, radio button, check box, etc. The leaf nodes exist at the lowest level in the tree hierarchy. Each of the node present in the scene graphs represents classes of javafx.scene package therefore we need to import the package into our application in order to create a full featured javafx application.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 5/9 Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX School of Computer Science and Engineering July VIT-AP 6, 2022 University 6/9 Creating Our first JavaFX Application
Step 1: Extend javafx.application.Application and override start()
start() method is the starting point of constructing a JavaFX application therefore we need to first override start method of javafx.application.Application class. Object of the class javafx.stage.Stage is passed into the start() method therefore import this class and pass its object into start method. JavaFX.application.Application needs to be imported in order to override start method. Step 2: Create a Button A button can be created by instantiating the javafx.scene.control.Button class. For this, we have to import this class into our code. Pass the button label text in Button class constructor.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 7/9 Step 3: Create a layout and add button to it JavaFX provides the number of layouts. We need to implement one of them in order to visualize the widgets properly. It exists at the top level of the scene graph and can be seen as a root node. All the other nodes (buttons, texts, etc.) need to be added to this layout. In this application, we have implemented StackPane layout. It can be implemented by instantiating javafx.scene.layout.StackPane class. Step 4: Create a Scene The layout needs to be added to a scene. Scene remains at the higher level in the hierarchy of application structure. It can be created by instantiating javafx.scene.Scene class. We need to pass the layout object to the scene class constructor.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 8/9 Step 5: Prepare the Stage javafx.stage.Stage class provides some important methods which are required to be called to set some attributes for the stage. We can set the title of the stage. We also need to call show() method without which, the stage won’t be shown. Step 6: Create an event for the button As our application prints hello world for an event on the button. We need to create an event for the button. For this purpose, call setOnAction() on the button and define a anonymous class Event Handler as a parameter to the method. Inside this anonymous class, define a method handle() which contains the code for how the event is handled. Step 7: Create the main method Till now, we have configured all the necessary things which are required to develop a basic JavaFX application but this application is still incomplete. We have not created main method yet. Hence, at the last, we need to create a main method in which we will launch the application i.e. will call launch() method and pass the command line arguments (args) to it.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1JavaFX
School of Computer Science and Engineering July VIT-AP 6, 2022 University 9/9