Untitled (1)-------------------------------------------------.pptx

poojakkamakshi2006 5 views 97 slides Mar 02, 2025
Slide 1
Slide 1 of 97
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97

About This Presentation

-------------------------


Slide Content

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

J a v aFX JavaFX is a Java library used to develop Desktop applications as well as Rich Internet Applications (RIA). The applications built in JavaFX, can run on multiple platforms including Web, Mobile and Desktops. Predecessor of Java FX Applet Swing

JavaFX Packages The JavaFX framework is contained in packages that begin with the javafx prefix. javafx.application javafx.stage javafx.scene javafx.scene.layout

Components Stage - It is a space for app, container for scene , @top-level. Scene - It a container for the items present in scene. JavaFX required at least one stage and scene to display GUI on the screen. First stage called primary stage. Scene Contains GUI elements like controls, text, and graphics

Nodes & Scene Graph elements of scene is Node The node. Each node associated with another node. Nodes are arranged in tree structure has parent child relationship Scene graph It is root node

L a y outs It is concept of placing elements in scene. Layouts are called as panes Flow Pane Grid Pane Border Pane All the panes inherits the node

JavaFX Lifecycle Methods These methods available in Application Class (javafx.application - package) void init( ) Initializes app - like constructor abstract void start(Stage primaryStage) It helps to construct or set scene and clean up procedures void stop( ) App stopped done Launch method is called from main function.

Code package oop . unitv ; import javafx . application . * ; import javafx . stage . * ; public class App extends Application { public void init () { System . out . println ( "init method Called" ) ; } public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; stage . show () ; } public void stop () { System . out . println ( "stop method Called" ) ; } public static void main ( String [] args ) { launch () ; } }

Code Only title bar is visible no scene to display

Code package oop . unitv ; import javafx . application . * ; import javafx . stage . * ; import javafx . scene . * ; import javafx . scene . layout . * ; public class App extends Application { public void init () { System . out . println ( "init method Called" ) ; } public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } public void stop () { System . out . println ( "stop method Called" ) ; } public static void main ( String [] args ) { launch () ; } }

Code App titled “ MyApp ” showing scene ( area 400 x 300 pixels ) in FlowPane Layout

Scene Graph Scene Graph exists at the lowest level of the hierarchy. It can be seen as the collection of v arious 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. These elements available in package javafx.scene.control

Code import javafx . scene . layout . * ; import javafx . scene . control . * ; public class App extends Application { public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Label label = new Label ( "This a test label" ) ; rootNode . getChildren (). add ( label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } public static void main ( String [] args ) { launch () ; } } It returns an Obser v ableList

Code Label displayed

Code public void start(Stage stage) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Label label01 = new Label ( "Label 01" ) ; Label label02 = new Label ( "Label 02" ) ; Label label03 = new Label ( "Label 03" ) ; rootNode . getChildren (). add ( label01 ) ; rootNode . getChildren (). add ( label02 ) ; rootNode . getChildren (). add ( label03 ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

Code

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

E v ents GUI application programs are usually event driven. JavaFX follows an delegation event model for handling events. A user-interface element delegates the processing of an event to a separate event handler.

The Event Class It is a class which is available in javafx.event package. When a JavaFX event is generated, it is encapsulated within an Event instance. Many Event Classes Inherits Event Classe MouseEvent KeyEvent DragEvent WindowEvent

The EventHandler Interface A class handles the event is called event handler. Event handlers implements the EventHandler Interfaces. Interface EventHandler<T extends Event> void handle(T eventObj ) Method need to be implemented Node has method to addEventHandler() & removeEventHandler() to assign or remove an event handler object to a particular node.

The Event Dispatch Chain JavaFX events are processed using event dispatch chain. It is a path from stage to target node. Two phases of event processing Capturing the event The passed from the top element down the chain to the target of the event Event bubbling Event handlers are called during the event bubbling phase. Event Filter It is a special handler handles the event before it reaches target node. It is also possible for a node in the chain to consume an event, which prevents it from being processed further.

The EventFilter JavaFX events are processed using event dispatch chain. It is a path from stage to target node. Two phases of event processing Capturing the event The passed from the top element down the chain to the target of the event are called during the e v ent bubbling Event bubbling E v ent handlers phase.

Code import javafx . application . * ; import javafx . stage . * ; import javafx . scene . * ; import javafx . scene . layout . * ; import javafx . scene . control . * ; import javafx . event . * ; public class App extends Application { public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Button button = new Button ( "Click Me" ) ; button . setOnAction ( new EventHandler < ActionEvent >() { public void handle ( ActionEvent e ) { System . out . println ( "Button Clicked" ) ; } } ) ; rootNode . getChildren (). add ( button ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } public static void main ( String [] args ) { launch () ; } }

Code Button Clicked

Code public class App extends Application { public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; label . setText ( "Button Clicked" ) ; Button button = new Button ( "Click Me" ) ; Label label = new Label ( "Button Not Yet Clicked" ) ; button . setOnAction ( new EventHandler < ActionEvent >() { public void handle ( ActionEvent e ) { } } ) ; rootNode . getChildren (). add ( button ) ; rootNode . getChildren (). add ( label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

Code

It is generated by a keyboard whenever a key is pressed. The object of KeyEvent class is generated while pressing the key and handler will handle it. KeyEvent is packaged under javafx.scene.input Three Types of Key Events KEY_PRESSED KEY_RELEASED KEY_TYPED A Node or Scene can respond for the event by registering event handler final void setOnKeyPressed(EventHandler handler) final void setOnKeyReleased(EventHandler handler) final void setOnKeyTyped(EventHandler handler) final String getCharacter( ) is a method user to get the character pressed Key Event

Code public class App extends Application { public void start ( Stage stage ) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Label label = new Label ( "No Key Pressed" ) ; rootNode . getChildren (). add ( label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; scene . setOnKeyTyped ( new EventHandler < KeyEvent >() { public void handle ( KeyEvent e ) { label . setText ( "Key " + e . getCharacter ()+ " pressed" ) ; } } ) ; stage . setScene ( scene ) ; stage . show () ; }

Code

KEYCODE For key-pressed and key-released events, you can obtain the key code associated with the event. Key codes are enumeration constants defined by KeyCode, which is packaged in javafx.scene.input. A large number of codes are defined because they represent the various keys on a keyboard. Key Event

Key Event

Code Scene scene = new Scene(rootNode,400,300) ; scene.setOnKeyPressed(new EventHandler<KeyEvent>() { public void handle ( KeyEvent e ) { System . out . println ( "Key Code :" + e . getCode ()) ; switch ( e . getCode ()) { case RIGHT : label . setText ( "RIGHT Key Pressed" ) ; break ; case LEFT : label . setText ( "LEFT Key Pressed" ) ; break ; case F1 : label . setText ( "F1 Key Pressed" ) ; break ; case UP : label . setText ( "UP Key Pressed" ) ; break ; } } } ) ;

Code

Mouse e v ents are represented by the MouseE v ent class, which is packaged in javafx.scene.input. By Implementing MouseEvent handler we can respond for mouse action like MOUSE_CLIKED MOUSE_DRAGGED Mouse Event

Code Scene scene = new Scene(rootNode, 400, 300) ; scene.setOnMouseClicked(new EventHandler < MouseEvent > () { public void handle ( MouseEvent e ) { label . setText ( "Mouse Clicked" ) ; System . out . println ( "Click Count: " + e . getClickCount ()) ; System . out . println ( "X coordinate: " + e . getSceneX ()) ; System . out . println ( "Y coordinate: " + e . getSceneY ()) ; } } ) ; scene.setOnMouseMoved(new EventHandler < MouseEvent > () { public void handle ( MouseEvent e ) { label . setText ( e . getEventType (). getName ()) ; } } ) ;

Code

Return Type Value Description static int BUTTON1 Indicates mouse button #1; used by getButton(). static int BUTTON2 Indicates mouse button #2; used by getButton(). static int BUTTON3 Indicates mouse button #3; used by getButton(). static int MOUSE_CLICKED The "mouse clicked" event. static int MOUSE_DRAGGED The "mouse dragged" event.

Return Type Value Description static int MOUSE_ENTERED The "mouse entered" event. static int MOUSE_EXITED The "mouse exited" event. static int MOUSE_FIRST The first number in the range of ids used for mouse events. static int MOUSE_LAST The last number in the range of ids used for mouse events. static int MOUSE_MOVED The "mouse moved" event.

Event Filter Event Filters process the events during the Event capturing phase of event handling. Event Filters need to be registered with the node in order to provide the event handling logic for the event generated on the node. JavaFX enables us to register a single event filter for more than one node and more than one event types. In simple terms we can create filter that mapped to capture a particular event in keyboard or mouse events. Method final void addEventFilter (EventType eType, EventHandler handler)

Code Scene scene = new Scene(rootNode, 400, 300) ; scene.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler < MouseEvent > () { public void handle ( MouseEvent e ) { label . setText ( "Mouse Clicked" ) ; System . out . println ( "Click Count: " + e . getClickCount ()) ; System . out . println ( "X coordinate: " + e . getSceneX ()) ; System . out . println ( "Y coordinate: " + e . getSceneY ()) ; } } ) ;

Code

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox , ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

CheckB o x The Check B o x is used to provide more than one choices to the user. It can be used in a scenario where the user is prompted to select more than one option or the user wants to select multiple options. javafx.scene.control.CheckBox

CheckB o x Checkbox has three states checked unchecked indeterminate / undefined CheckBox generates an action event when it is clicked. Need to call setOnAction() to assign handler To check the status we need to call isSelected() method

Code CheckB o x public void start(Stage stage) { System . out . println ( "start method Called" ) ; stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; Label label = new Label ( "Nothing Selected" ) ; CheckBox c1 = new CheckBox ( "Apple" ) ; c1 . setIndeterminate ( true ) ; rootNode . getChildren (). addAll ( c1 , label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; c1 . setOnAction ( new EventHandler < ActionEvent > () { public void handle ( ActionEvent e ) { if ( c1 . isSelected ()) { label . setText ( "Apple Checked" ) ; } else { label . setText ( "Apple Not Selected" ) ; } } } ) ; stag e . setScen e ( scen e ) ; stage . show () ; }

Toggle Button A ToggleButton is a specialized control which has the ability to be selected. Typically a ToggleButton is rendered similarly to a Button. It can be in two states ON/OFF.

Toggle Button A ToggleButton is a specialized control which has the ability to be selected. ToggleButton is a control with a Boolean indicating whether it is selected. Typically a ToggleButton is rendered similarly to a Button. It can be in two states ON/OFF.

Radio Button RadioButtons are mainly used to create a series of items where only one can be selected. When a Radio button is pressed and released an Action event is sent, this Action Event can be handled using an Event Handler.

Code public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; RadioButton rb1 RadioButton rb2 RadioButton rb3 = new RadioButton ( "Apple" ) ; = new RadioButton ( "Mango" ) ; = new RadioButton ( "Orange" ) ; Label label = new Label ( "No Button Selected" ) ; rootNode . getChildren (). addAll ( rb1 , rb2 , rb3 , label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } All buttons are selectable but it is not the usual case with radio button

Code public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; FlowPane rootNode = new FlowPane () ; RadioButton rb1 RadioButton rb2 RadioButton rb3 = new RadioButton ( "Apple" ) ; = new RadioButton ( "Mango" ) ; = new RadioButton ( "Orange" ) ; ToggleGroup tg = new ToggleGroup () ; rb1 . setToggleGroup ( tg ) ; rb2 . setToggleGroup ( tg ) ; rb3 . setToggleGroup ( tg ) ; Label label = new Label ( "No Button Selected" ) ; rootNode . getChildren (). addAll ( rb1 , rb2 , rb3 , label ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } Problem Fixed

Radio Button Toggle Group enables the radio buttons can be grouped but it needs a handler to respond for the action event. The interface is ChangeListener It is notified whenever the value of an ObservableValue changes. It can be registered and unregistered with respectively ObservableValue.addListener(ChangeListener) ObservableValue.removeListener(ChangeListener) An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable 's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.

Code Radio Button RadioButton rb1 = new RadioButton("Apple") ; RadioButton rb2 = new RadioButton("Mango") ; RadioButton rb3 = new RadioButton("Orange") ; ToggleGroup tg = new ToggleGroup() ; Label label = new Label("No Button Selected") ; rb1.setToggleGroup(tg) ; rb2.setToggleGroup(tg) ; rb3.setToggleGroup(tg) ; tg.selectedToggleProperty().addListener(new ChangeListener < Toggle > () { public void changed ( ObservableValue << ? extends Toggle > ob , Toggle o , Toggle n ) { RadioButton rb = ( RadioButton ) tg . getSelectedToggle () ; if ( rb != null ) { String s = rb . getText () ; // change the label label . setText ( s + " selected" ) ; } } } ) ;

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane . Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

List V iew A list view is a scrollable list of items from which you can select desired items. It enables the user to choose one or more options from a predefined list of options. javafx.scene.control.ListView

Code CheckB o x public void start ( Stage stage ) { stag e . setTitl e ( "MyApp " ); FlowPane rootNode = new FlowPane (); Label label = new Label ( "Nothing Selected" ); ObservableList < String > fruits = FXCollections . observableArrayList ( "Apple" , "Mango" , "Orange" ); ListView < String > listView = new ListView < String >( fruits ); listView . getSelectionModel (). selectedItemProperty (). addListener ( new ChangeListener < String >() { @ Override public void changed ( ObservableValue < ? extends String > observable , String oldValue , String newValue ) { label . setText ( newValue + " selected" ); } }); rootNode . getChildren (). addAll ( listView , label ); Scene scene = new Scene ( rootNode , 400 , 300 ); stage . setScene ( scene ); stage . show (); } List items to be displayed Whenever a list item selected the handler will be triggered

ComboBox The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. javafx.scene.control.ComboBox

Code CheckB o x public void start ( Stage stage ) { stage . setTitle ( "MyApp" ); FlowPane rootNode = new FlowPane (); Label label = new Label ( "Nothing Selected" ); ObservableList < String > fruits = FXCollections . observableArrayList ( "Apple" , "Mango" , "Orange" ); ComboBox comboBox = new ComboBox < String >( fruits ); comboBox. setEditable( true ); comboBox . setOnAction ( new EventHandler < ActionEvent >() { @ Override public void handle ( ActionEvent event ) { label . setText ( comboBox . getValue (). toString ()); } }); rootNode . getChildren (). addAll ( comboBox , label ); Scene scene = new Scene ( rootNode , 400 , 300 ); stage . setScene ( scene ); stage . show (); } List items to be displayed Whenever item is selected and ENTER key pressed handler will get the value selected User can type the choice which is not predefined

Editing Enabled

ChoiceB o x The J a v aFX ChoiceB o x control enables users to choose an option from a predefined list of choices. It won’t allow user choice to be typed. javafx.scene.control.ChoiceBox

Code CheckB o x public void start ( Stage stage ) { stage . setTitle ( "MyApp" ); FlowPane rootNode = new FlowPane (); Label label = new Label ( "Nothing Selected" ); ObservableList < String > fruits = FXCollections . observableArrayList ( "Apple" , "Mango" , "Orange" ); ChoiceBox choiceBox = new ChoiceBox < String >( fruits ); choiceBox . getSelectionModel (). selectedItemProperty (). addListener ( new ChangeListener < String >() { @ Override public void changed ( ObservableValue < ? extends String > observable , String oldValue , String newValue ) { label . setText ( newValue + " selected" ); } }); rootNode . getChildren (). addAll ( choiceBox , label ); Scene scene = new Scene ( rootNode , 400 , 300 ); stage . setScene ( scene ); stage . show (); }

TextControl - TextField TextField class is a part of JavaFX package. It is a component that allows the user to enter a line of unformatted text, it does not allow multi-line input it only allows the user to enter a single line of text.

Code CheckB o x public void start ( Stage stage ) { stage . setTitle ( "MyApp" ); FlowPane rootNode = new FlowPane (); Label label = new Label ( "No Text" ); TextField textField = new TextField (); textField . setPrefColumnCount ( 20 ); textField . setOnAction ( new EventHandler < ActionEvent >() { @ Override public void handle ( ActionEvent event ) { label . setText ( textField . getText ()); } }); rootNode . getChildren (). addAll ( textField , label ); Scene scene = new Scene ( rootNode , 400 , 300 ); stage . setScene ( scene ); stage . show (); }

Code CheckB o x public void start ( Stage stage ) { stage . setTitle ( "MyApp" ); FlowPane rootNode = new FlowPane (); Label label = new Label ( "button not clicked" ); TextField textField = new TextField (); Button button = new Button ( "Click" ); textField . setPrefColumnCount ( 20 ); button . setOnAction ( new EventHandler < ActionEvent >() { @ Override public void handle ( ActionEvent event ) { label . setText ( textField . getText ()); } }); rootNode . getChildren (). addAll ( textField , button , label ); Scene scene = new Scene ( rootNode , 400 , 300 ); stage . setScene ( scene ); stage . show (); } TextControl - TextField & Button

ScrollPane The Scroll P ane all o ws specification of the scroll bar policy, when displ a y ed: al w a ys, ne v er, which determines scroll bars are or only when they are needed. The scroll bar policy can be specified independently for the horizontal and vertical scroll bars.

Code CheckB o x public void start(Stage stage) { stag e . set T itle ( "MyApp " ) ; FlowPane rootNode = new FlowPane () ; Label label = new Label ( "JavaFX is a Java library used to" + " \n develop Desktop applications as well" + " \ n a s Ric h Interne t Application s (RIA). " + " \n The applications built in JavaFX, can " + " \n run on multiple platforms including Web," + " \n Mobile and Desktops." ) ; ScrollPane scrollPane = new ScrollPane ( label ) ; scrollPane . setPrefWidth ( 120 ) ; scrollPane . setPrefHeight ( 80 ) ; scrollPane . setPannable ( true ) ; Button button = new Button ( "Reset Scroll Pane" ) ; button . setOnAction ( new EventHandler < ActionEvent >() { @Override public void handle ( ActionEvent event ) { scrollPane . setVvalue ( ) ; scrollPane . setHvalue ( ) ; } } ) ; rootNode . getChildren (). addAll ( scrollPane , button ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

L a y outs This arrangement of the components within the container is called the Layout of the container. FlowPane HBox and VBox BorderPane StackPane GridPane. javafx.scene.control.ListView

HB o x HBox in the layout in our application, all the nodes are set in a single horizontal row.

Code CheckB o x public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; HBox rootNode = new HBox () ; rootNode . setSpacing ( 10 ) ; TextField textField = new TextField () ; Button ok = new Button ( "OK" ) ; Button reset = new Button ( "RESET" ) ; rootNode . getChildren (). addAll ( textField , ok , reset ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

VB o x VBox as the layout in our application, all the nodes are set in a single vertical column.

Code CheckB o x public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; VBox rootNode = new VBox () ; rootNode . setSpacing ( 10 ) ; TextField textField = new TextField () ; Button ok = new Button ( "OK" ) ; Button reset = new Button ( "RESET" ) ; rootNode . getChildren (). addAll ( textField , ok , reset ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

Border P ane Border P ane, the nodes are arranged in the T op, Left, Right, Bottom and Center positions.

Code CheckB o x public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; BorderPane rootNode = new BorderPane () ; Button topButton = new Button ( "TOP" ) ; Button bottomButton = new Button ( "BOTTOM" ) ; Button leftButton = new Button ( "LEFT" ) ; Button rightButton = new Button ( "RIGHT" ) ; Button centerButton = new Button ( "CENTER" ) ; rootNode . setTop ( topButton ) ; rootNode . setBottom ( bottomButton ) ; rootNode . setLeft ( leftButton ) ; rootNode . setRight ( rightButton ) ; rootNode . setCenter ( centerButton ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

Stack P ane In StackPane, the nodes are arranged on top of another, just like in stack. The node added first is placed at the bottom of the stack and the next node is placed on top of it.

Code CheckB o x public void start(Stage stage) { stage . setTitle ( "MyApp" ) ; StackPane rootNode = new StackPane () ; Button button1 = new Button ( "BUTTON1" ) ; Button button2 = new Button ( "BUTTON2" ) ; Button button3 = new Button ( "BUTTON3" ) ; rootNode . getChildren (). addAll ( button1 , button2 , button3 ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stag e . setScen e ( scen e ) ; stage . show () ; }

Grid P ane all the nodes that are added to it are arranged in a way that they form a grid of rows and columns.

Code CheckB o x public void start(Stage stage) { stag e . setTitl e ( "MyApp " ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; } GridPane rootNode = new GridPane () ; rootNod e . setVga p ( 5 ) ; rootNod e . setHga p ( 5 ) ; Button button1 = new Button ( "BUTTON1" ) ; Button button2 = new Button ( "BUTTON2" ) ; Button button3 = new Button ( "BUT d T g O g N g 3" ) ; Button button4 = new Button ( "BUTTON4" ) ; rootNode . add ( button1 , , ) ; rootNode . add ( button2 , , 1 ) ; rootNode . add ( button3 , 1 , ) ; rootNode . add ( button4 , 1 , 1 ) ;

UNIT V JAVAFX EVENT HANDLING, CONTROLS AND COMPONENTS JAVAFX Events and Controls: Event Basics – Handling Key and Mouse Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView – ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts – FlowPane – HBox and VBox – BorderPane – StackPane – GridPane. Menus – Basics – Menu – Menu bars – MenuItem.

Menus JavaFX provides a Menu class to implement menus. Menu is the main component of a any application. In JavaFX, javafx.scene.control.Menu class provides all the methods to deal with menus. This class needs to be instantiated to create a Menu.

Code CheckB o x public void start(Stage stage) { BorderPane rootNode = new BorderPane () ; MenuBar menubar = new MenuBar () ; Menu FileMenu = new Menu ( "File" ) ; MenuItem filemenu1 = new MenuItem ( "new" ) ; MenuItem filemenu2 = new MenuItem ( "Save" ) ; MenuItem filemenu3 = new MenuItem ( "Exit" ) ; Menu EditMenu = new Menu ( "Edit" ) ; MenuItem EditMenu1 = new MenuItem ( " d C g u g t g " ) ; MenuItem EditMenu2 = new MenuItem ( "Copy" ) ; MenuItem EditMenu3 = new MenuItem ( "Paste" ) ; EditMenu . getItems (). addAll ( EditMenu1 , EditMenu2 , EditMenu3 ) ; FileMenu . getItems (). addAll ( filemenu1 , filemenu2 , filemenu3 ) ; menubar . getMenus (). addAll ( FileMenu , EditMenu ) ; rootNode . setTop ( menubar ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }

Menus - Action The MenuItem class inherits a property named onAction from the javafx.scene.control.ButtonBase class, which is of the type ObjectProperty<EventHandler<ActionEvent>>. This property represents the action that is invoked whenever you press the button. You can set the value to this property using the setOnAction() method.

Code CheckB o x public void start(Stage stage) { BorderPane rootNode = new BorderPane () ; MenuBar menubar = new MenuBar () ; Menu FileMenu = new Menu ( "File" ) ; MenuItem filemenu1 = new MenuItem ( "new" ) ; MenuItem filemenu2 = new MenuItem ( "Save" ) ; MenuItem filemenu3 = new MenuItem ( "Exit" ) ; Menu EditMenu = new Menu ( "Edit" ) ; MenuItem EditMenu1 = new MenuItem ( "Cut" ) ; MenuItem EditMenu2 = new MenuItem ( "Copy" ) ; MenuItem EditMenu3 = new MenuItem ( "Paste" ) ; Label output = new Label ( "Nothing Selected" ) ; filemenu1 . setOnAction ( new EventHandler < ActionEvent >() { public void handle ( ActionEvent event ) { output . setText ( "new selected" ) ; } }; enu 3 ) ; enu 3 ) ; EditMenu . getItems (). addAll ( EditMenu1 , EditMenu2 , EditM FileMenu . getItems (). addAll ( filemenu1 , filemenu2 , filem menubar . getMenus (). addAll ( FileMenu , EditMenu ) ; rootNode . setTop ( menubar ) ; rootNode . setBottom ( output ) ; Scene scene = new Scene ( rootNode , 400 , 300 ) ; stage . setScene ( scene ) ; stage . show () ; }
Tags