Chapter 14 Java FX Basics Dr Clincy Lecture

Chapter 14 Java. FX Basics Dr. Clincy - Lecture 1

Java GUI Programming • What is a graphical user interface (GUI) ? And what is GUI programming ? • When Java was initially introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). • AWT is fine for developing simple GUIs, but not good for developing comprehensive GUI projects. In addition, AWT is prone to platform-specific bugs. • Later, the AWT was replaced by a more robust, versatile, and flexible library known as Swing components depend less on the target platform and use less resources. • With the release of Java 8, Swing is replaced by a completely new GUI platform known as Java. FX. • Java. FX is a newer framework for developing Java GUI programs. Dr. Clincy - Lecture 2

Basic Structure of Java. FX § § javafx. application. Application class defines the essential framework for writing Java. FX programs Every Java. FX program is defined in a class that extends javafx. application. Application Stage object is automatically created by JVM when the application is launched – considered the window – you can have multiple stages. Scene object is the container for the content – considered the frame in the window – can have only one scene per stage or window Button object (or node) is a visual component such as a shape, image, UI control, groups, and panes. Used to invoke some type of action Container classes called panes can be used to help layout the nodes within a scene. Control refers to a label, check box, radio button, text field and etc. A group can be used to group a set of nodes Dr. Clincy - Lecture 3

Panes, UI Controls, and Shapes ( 1 ) One Scene per Stage ( 2 ) A Scene can contain a Control, Group or Pane ( 3 ) A Pane or Group can contain any subtype of Node Dr. Clincy - Lecture ( 4 ) Will cover the various types of Panes later 4

Basic Structure of Java. FX Program Every Java. FX program extends javafx. application. Application The main class overrides the start method defined in javafx. application. Application and JVM constructs an instance of the class and invoke the start method. Create a button object and places it in a Scene object created using the constructor – specifies width and height and places node in scene The Stage object is automatically created by JVM when the app is launched Name the Stage, set the scene in the stage, and display the stage. Output of program displays a button in the window Dr. Clincy - Lecture 5 The launch method is static method used for launching stand-alone Java. FX apps. Not needed if you run it from a command line.

Java. FX Program w/Multiple Stages Every Java. FX program extends javafx. application. Application The main class overrides the start method defined in javafx. application. Application and JVM constructs an instance of the class and invoke the start method. The first Scene object is created using the constructor – specifies width and height and places button in scene The first Stage object is automatically created by JVM when the app is launched Name the first Stage, set the scene in the stage, and display the stage. A new stage is created. Name the second Stage, set the scene in the stage, places button in Scene, and display the stage. Dr. Clincy - Lecture Output of program displays multiple stages 6 The launches Java. FX app. Identical main method for all every Java. FX app.

Java. FX Program w/Stage within Stage Every Java. FX program extends javafx. application. Application The main class overrides the start method defined in javafx. application. Application and JVM constructs an instance of the class and invoke the start method. The first Scene object is created using the constructor – specifies width and height and places button in scene (200 and 250) Name the second Stage, set the scene in the stage, places button in Scene, and display the stage. The second Scene object is created using the constructor – specifies width and height and places button in scene (100 and 100) Output of program displays overlapping stages Dr. Clincy - Lecture 7

Java. FX Program w/Button in Pane For Pane Create a Pane Add button to created Pane Add Pane to Scene Name the Stage, set the scene in the stage, and display the stage. Output of program displays a button in the center of the pane Lines 11 and 12 could be replaced with: Stack. Pane pane = new Stack. Pane (new Button(“OK”)); Dr. Clincy - Lecture 8

Java. FX Program w/Circle in Pane For Circle and Color Create a Circle Set Circle properties Set Circle’s center at (100, 100) Measurements are in pixels The color of the circle’s fill Create Pane and add Circle Add Pane to Scene Output of program displays a circle in the center of the scene Dr. Clincy - Lecture 9

Positioning a Shape Java’s Approach Dr. Clincy - Lecture Conventional Approach 10

Cover Lab 9 Dr. Clincy - Lecture 11
- Slides: 11