Button Control
§ Nearly all JavaFX
controls generate events.
§ most commonly used
control is the button,
§
button events are some of the
most frequently handled in a JavaFX program.
§
In JavaFX, the push button control is provided by
the Button class, which is in javafx.scene.control.
§ Button inherits a long list of base classes that includes ButtonBase, Labeled, Region, Control,
Parent, and Node.
§ Most of its
functionality comes from its base classes.
§ Buttons can contain text, graphics, or both.
§ Button supports a wide array of options, but we will use the
default form.
Button has three constructors.
1. Button(): creates a
button with an empty string for its label.
2. Button(String t):
creates a button with the specified text as its label.
3. Button(String t, Node
g): creates a button with the specified text and icon for its label.
// JavaFX controls
// Buttons
import java.io.*;
import java.net.*;
import
javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import
javafx.scene.layout.*;
import
javafx.scene.control.*;
import javafx.event.*;
import
javafx.geometry.*;
import
javafx.scene.image.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public class JfxBtn
extends Application
{
public static void main(String[] args)
{
// Start the JavaFX application by calling launch().
launch(args);
}
// Override the start() method.
public void start(Stage myStage)
{
try
{
// Give the stage a title.
myStage.setTitle("JavaFx controls - Buttons");
// Use a FlowPane for the root node.
FlowPane fp = new FlowPane(25,10);
// Center the controls in the scene.
fp.setAlignment(Pos.CENTER);
// Create a scene.
Scene myScene = new Scene(fp, 500, 100);
// Set the scene on the stage.
myStage.setScene(myScene);
// Create Three push buttons.
Button Btn1, Btn2, Btn3;
Btn1 = new Button();
Btn1.setMinWidth(100);
Btn2 = new Button("Named button");
Btn2.setMinWidth(100);
// Use input stream to get image
FileInputStream input = new
FileInputStream("d:\\Subjects\\OOP R2021\\Jpgms\\JavaFX\\home.png");
// create a image
Image i = new Image(input);
// create a image View and resize icon
ImageView iv = new ImageView(i);
iv.setFitHeight(25);
iv.setFitWidth(25);
// create a button
Btn3 = new Button(" Home", iv);
Btn3.setMinWidth(100);
// Add the buttons to the scene graph.
fp.getChildren().addAll(Btn1, Btn2, Btn3);
}
catch(Exception ex)
{}
// Show the stage and its scene.
myStage.show();
}
}
Demonstrating Event
Handling and the Button
//
Event Handling
//
Buttons and labels
import
javafx.application.*;
import
javafx.scene.*;
import
javafx.stage.*;
import
javafx.scene.layout.*;
import
javafx.scene.control.*;
import
javafx.event.*;
import
javafx.geometry.*;
public
class JfxEH extends Application
{
Label respLbl;
public static void main(String[] args)
{
// Start the JavaFX application by
calling launch().
launch(args);
}
// Override the start() method.
public void start(Stage myStage)
{
// Give the stage a title.
myStage.setTitle("Event handling
- Buttons and labels");
// Use a FlowPane for the root node.
FlowPane fp = new FlowPane(25,10);
// Center the controls in the scene.
fp.setAlignment(Pos.CENTER);
// Create a scene.
Scene myScene = new Scene(fp, 300,
100);
// Set the scene on the stage.
myStage.setScene(myScene);
// Create a label.
respLbl = new Label("Select a Button");
// Create two push buttons.
Button Btn1 = new
Button("IT");
Btn1.setMinWidth(50);
Button Btn2 = new
Button("CSE");
Btn2.setMinWidth(50);
// Handle the action events for the
First button.
Btn1.setOnAction(new
EventHandler<ActionEvent>(){
public void handle(ActionEvent ae)
{
respLbl.setText("IT was selected.");
}
} ) ;
// Handle the action events for the
Second button.
Btn2.setOnAction(new
EventHandler<ActionEvent>(){
public void handle(ActionEvent ae)
{
respLbl.setText("CSE was
selected.");
}
} ) ;
// Add the label and buttons to the
scene graph.
fp.getChildren().addAll(Btn1, Btn2,
respLbl);
// Show the stage and its scene.
myStage.show();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu