CH 2 Graphical User Interface
CH 2 Graphical User Interface
Button Panel
List
Checkbox
Choice
Label
TextComponent TextField
TextArea
Or:
b1.addActionListener
(new MyButtonListener ( ));
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
}
November 24, 2024 Files And Streams 27
JAVAFX for GUI
• JavaFX apprlication
• Step 2: Create a Button
• package application;
• import javafx.application.Application;
• importjavafx.scene.control.Button;
• import javafx.stage.Stage;
• public class Hello_World extends Application{
•
• @Override
• public void start(Stage primaryStage) throws Exception {
• // TODO Auto-generated method stub
• Buttonbtn1=newButton("Say, Hello World");
•
• }
•
• } 24, 2024
November Files And Streams 28
JAVAFX for GUI
• JavaFX apprlication
• Step 3: Create a layout and add button to it
package application;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
public class Hello_World extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Button btn1=new Button("Say, Hello World");
StackPane root=new StackPane();
root.getChildren().add(btn1);
}
}
BorderPane Organizes nodes in top, left, right, centre and the bottom of the screen.
FlowPane Organizes the nodes in the horizontal rows according to the available
horizontal spaces. Wraps the nodes to the next line if the horizontal
space is less than the total width of the nodes
StackPane Organizes nodes in the form of a stack i.e. one onto another
VBox
November 24, 2024
Organizes nodes in a vertical column.
Files And Streams 34
JAVAFX for GUI
• JavaFX UI Layouts
• Steps to create layout
• Instantiate the respective layout class, for example, HBox root = new HBox();
• Setting the properties for the layout, for example, root.setSpacing(20);
• Adding nodes to the layout object, for
example, root.getChildren().addAll(<NodeObjects>);
• JavaFX Event Handlers
• Foreground events are mainly occurred due to the direct interaction of the user
with the GUI of the application. Such as clicking the button, pressing a key,
selecting an item from the list, scrolling the page, etc.
• Background events doesn't require the user's interaction with the application.
These events are mainly occurred to the operating system interrupts, failure,
operation completion, etc.
November 24, 2024 Files And Streams 35
JAVAFX for GUI
• JavaFX UI Layouts
• Steps to create layout
• Instantiate the respective layout class, for example, HBox root = new HBox();
• Setting the properties for the layout, for example, root.setSpacing(20);
• Adding nodes to the layout object, for
example, root.getChildren().addAll(<NodeObjects>);
• JavaFX Event Handlers
• Foreground events are mainly occurred due to the direct interaction of the user
with the GUI of the application. Such as clicking the button, pressing a key,
selecting an item from the list, scrolling the page, etc.
• Background events doesn't require the user's interaction with the application.
These events are mainly occurred to the operating system interrupts, failure,
operation completion, etc.
November 24, 2024 Files And Streams 36
JAVAFX for GUI
• JavaFX Event Handlers
• JavaFX provides the class javafx.event.Event
• example MouseEvent, KeyEvent, ScrollEvent, DragEvent, etc.
• Setter method example
• setOnDragDetected(EventHandler value )
• setOnDragDone(EventHandler value )
• setOnInputMethodTextChanged(EventHandler value )
• setOnKeyPressed(EventHandler value )
• setOnKeyReleased(EventHandler value )
• setOnMouseClicked(EventHandler value )
• ……
Thank You
November 24, 2024 Files And Streams 38