Radio Buttons
§ Radio buttons are typically used to manage a group of mutually exclusive buttons – only one button in a group can be
selected at any one time.
§ They are supported by the RadioButton class, which extends both ButtonBase and ToggleButton.
§ RadioButton
also implements the Toggle interface.
§ Radio buttons are the primary control
employed when the user must select only one option among several alternatives.
§ RadioButton
defines two constructors.
o
default
constructor.
o
Parameterized
constructor
RadioButton(String
str)
str is the label for the button.
§ To activate mutually exclusive nature – radio
buttons must be configured into a group.
§ Only one of the buttons in the group can be
selected at any time.
§ A button group is created by the ToggleGroup class, which is packaged in
javafx.scene.control.
§ ToggleGroup
provides only a default
constructor.
§ Radio buttons are added to the toggle group
by calling setToggleGroup( ) method
on the button.
final
void setToggleGroup(ToggleGroup tg)
§ After all radio buttons have been added to
the same group, their mutually exclusive behavior will be enabled.
§ When a RadioButton
is part of a ToggleGroup, an
action event is generated when the button is clicked only if the button was
previously cleared. Thus, an action event is generated only when a new button
is selected. If an already selected radio button is clicked, no event is
generated.
§ To set a RadioButton as selected in initial
state use setSelected( ) on the
button.
final
void setSelected(boolean state)
§
If
state is true, the button is selected. Otherwise, it is deselected.
ChangeListener: Watch for Changes in a Toggle
Group
§ Action events on each button or
§ listen to the entire toggle group for
changes.
o
one
piece of code can watch for a change to a group of radio buttons
§ When a change takes place, the event handler
can easily determine which radio button has been selected and take action
accordingly.
§ To use this approach,
1. register a ChangeListener on the toggle
group.
2. When a change occurs to the toggle group, the
listener is executed.
3. Inside the listener – determine which button
was selected.
4. Call selectedToggleProperty(
) on the toggle group and
5. Register the change listener for the toggle
group by calling the addListener( ) on
the returned object returned.
6.
ChangeListener defines only one method, called changed( ).
void changed(ObservableValue<?
extends T> changed,
T oldVal,
T newVal)
// Radio buttons
// Event handling and ChangeListener
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.geometry.*;
import java.lang.*;
import javafx.beans.value.*;
public class JfxChk extends Application
{
public
static void main(String []args)
{
launch(args);
}
public
void init(){}
public
void stop(){}
public
void start(Stage myStage)
{
myStage.setTitle("Radio
buttons");
FlowPane
fp = new FlowPane(Orientation.VERTICAL, 25, 35);
fp.setAlignment(Pos.CENTER);
Label
L1 = new Label("Select Gender");
Label
L2 = new Label("Event Handler.");
Label
L3 = new Label("Change Listener.");
RadioButton
rb1 = new RadioButton("Male");
RadioButton
rb2 = new RadioButton("Female");
ToggleGroup
tg = new ToggleGroup();
rb1.setToggleGroup(tg);
rb2.setToggleGroup(tg);
fp.getChildren().addAll(L1,
rb1, rb2, L2, L3);
Scene
myScene = new Scene(fp, 400, 300);
rb1.setOnAction(new
EventHandler<ActionEvent>(){
public
void handle(ActionEvent ae)
{
L2.setText("Event
- Radio button Male is clicked.");
}
});
rb2.setOnAction(new
EventHandler<ActionEvent>(){
public
void handle(ActionEvent ae)
{
L2.setText("Event
- Radio button Female is clicked.");
}
});
tg.selectedToggleProperty().addListener(new
ChangeListener<Toggle>()
{
public
void changed(ObservableValue<? extends Toggle> changed,
Toggle oldval, Toggle newval)
{
RadioButton
rb = (RadioButton) newval;
L3.setText("Value
of radio button "+rb.getText()+" is changed.");
}
});
myStage.setScene(myScene);
myStage.show();
}
}
Sample output:
>javac --module-path "c:\Program
Files\Java\javafx-sdk-21.0.1\lib" --add-modules javafx.controls
JfxChk.java
>java --module-path "c:\Program
Files\Java\javafx-sdk-21.0.1\lib" --add-modules javafx.controls JfxChk
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu