JavaFX Control: CheckBox
§ a special type of button
§ Interactive control –
allows user to select or deselect a choice
§ Allows multiple choices
to be selected
§ Check Box supports three states – checked, unchecked and indeterminate (also called undefined).
§ Check Box defines two constructors.
o default constructor.
o Constructor with a
string that identifies the box.
CheckBox(String
str)
§
Check Box generates an action
event when it is clicked.
§
Create action event handler on a Check Box by
calling setOnAction( )
§ Obtain the state of a
check box by calling isSelected( ).
final boolean isSelected( )
//
JavaFX controls
//
CheckBox
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 JfxChkBox 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)
{
// Give the stage a title.
myStage.setTitle("JavaFx controls
- CheckBox");
// Use a FlowPane for the root node.
FlowPane fp = new
FlowPane(Orientation.VERTICAL, 20,20);
// Center the controls in the scene.
fp.setAlignment(Pos.CENTER);
//
Create a scene.
Scene myScene = new Scene(fp, 400,
200);
// Set the scene on the stage.
myStage.setScene(myScene);
// Creating a heading
Label heading = new
Label("Checkbox Demo");
// Create Three checkbox.
CheckBox CB1, CB2, CB3;
CB1 = new
CheckBox("Selected");
CB2 = new
CheckBox("Unselected");
CB3 = new
CheckBox("Indeterminate");
CB1.setSelected(true);
CB2.setSelected(false);
CB3.setIndeterminate(true);
// Add to the scene graph.
fp.getChildren().addAll(heading, CB1,
CB2, CB3);
// Show the stage and its scene.
myStage.show();
}
}
// JavaFX controls
// CheckBox and Event
handling
import
javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.event.*;
import
javafx.scene.layout.*;
import
javafx.scene.control.*;
import
javafx.geometry.*;
public class JfxCBoxEH
extends Application
{
CheckBox CB1, CB2, CB3;
Label heading, respText;
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("JavaFx controls - CheckBox and event
handling");
// Use a FlowPane for the root node.
FlowPane fp = new FlowPane(Orientation.VERTICAL, 20,20);
// Center the controls in the scene.
fp.setAlignment(Pos.CENTER_LEFT);
fp.setPadding(new Insets(0,0,0,35));
// Create a scene.
Scene myScene = new Scene(fp, 400, 200);
// Set the scene on the stage.
myStage.setScene(myScene);
// Creating a heading and response text
heading = new Label("Checkbox and Event Handling Demo");
respText = new Label("No Device selected.");
// Create Three checkbox.
CB1 = new CheckBox("Mouse");
CB2 = new CheckBox("Keyboard");
CB3 = new CheckBox("Touch Screen");
// Add to the scene graph.
fp.getChildren().addAll(heading, CB1, CB2, CB3, respText);
CB1.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae)
{
fnDisplay();
}
} ) ;
CB2.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae)
{
fnDisplay();
}
} ) ;
CB3.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae)
{
fnDisplay();
}
} ) ;
// Show the stage and its scene.
myStage.show();
}
void fnDisplay()
{
String s = "Selected devices : ";
if(CB1.isSelected())
s = s+"Mouse ";
if(CB2.isSelected())
s = s+"Keyboard ";
if(CB3.isSelected())
s = s+"Touch Screen ";
if(s.equals("Selected devices : "))
s = s+"None.";
respText.setText(s);
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu