0% found this document useful (0 votes)
9 views24 pages

JavaFX RadioButton and CheckBox Controls

The document provides an overview of RadioButton, CheckBox, and TextArea controls in JavaFX. It explains how to create and manage these controls, including grouping RadioButtons in ToggleGroups, responding to user interactions with event handlers, and using properties to track selections. Additionally, it details methods for configuring TextArea properties and enabling text wrapping.

Uploaded by

kouroshmaissamy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

JavaFX RadioButton and CheckBox Controls

The document provides an overview of RadioButton, CheckBox, and TextArea controls in JavaFX. It explains how to create and manage these controls, including grouping RadioButtons in ToggleGroups, responding to user interactions with event handlers, and using properties to track selections. Additionally, it details methods for configuring TextArea properties and enabling text wrapping.

Uploaded by

kouroshmaissamy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java FX: Advanced Controls (Part _01)

Lecture 04
RadioButton Controls (1 of 7)
• RadioButton controls allow the user to select one
choice from several possible options.
RadioButton Controls (2 of 7)
• The RadioButton class is in the
[Link] package.
RadioButton radio1 = new RadioButton("Choice 1");
RadioButton radio2 = new RadioButton("Choice 2");
RadioButton radio3 = new RadioButton("Choice 3");
RadioButton Controls (3 of 7)
• RadioButton controls are normally grouped
together in a toggle group.
• Only one of the RadioButton controls in a toggle
group may be selected at any time.
• Clicking on a RadioButton selects it and
automatically deselects any other RadioButton in
the same toggle group.
RadioButton Controls (4 of 7)
• To create a toggle group, you use the ToggleGroup
class, which is in the [Link]
package:
ToggleGroup myToggleGroup = new ToggleGroup();

• After creating a ToggleGroup object, you call each


RadioButton control’s setToggleGroup method to
add them to the ToggleGroup.
RadioButton Controls (5 of 7)
// Create some RadioButtons.
RadioButton radio1 = new RadioButton(“Option 1”);
RadioButton radio2 = new RadioButton(“Option 2”);
RadioButton radio3 = new RadioButton(“Option 3”);

// Create a ToggleGroup.
ToggleGroup radioGroup = new ToggleGroup();

// Add the RadioButtons to the ToggleGroup .


[Link](radioGroup);
[Link](radioGroup);
[Link](radioGroup);
RadioButton Controls (6 of 7)
• To determine whether a RadioButton is selected, you
call the RadioButton class’s isSelected method.

if ([Link]())
{
// Code here executes if the radio
// button is selected.
}
RadioButton Controls (7 of 7)
• You usually want one of the RadioButtons in a group
to be initially selected.
• You can select a RadioButton in code with the
RadioButton class’s setSelected method:

[Link](true);

• SetUserData(Object value) lets you attach any


extra information to a UI control.
• GetUserData() retrieves that information later.
Responding to RadioButton Clicks
• Events are triggered when the user selects a radio
button.
• You can detect which button is selected using event
handlers.
Choose how to react to user input:
• Add an ActionEvent handler to each RadioButton (fires
when the button is activated — clicked or keyboard-
enter).
• OR add a single listener to the ToggleGroup
(ChangeListener) (fires whenever the selected toggle
changes)
Responding to RadioButton Clicks
• If you want an action to take place immediately when the
user clicks a RadioButton, register an ActionEvent
handler with the RadioButton control.
• The process is the same as with the Button control.
• When to use: few radio buttons, simple logic, or different
handlers per button.
[Link](handler);
[Link](handler);
Responding to
RadioButton using ToggleGroup
• Responding to radio button clicks in JavaFX involves
using ToggleGroup and listening for changes in
the selectedToggleProperty.
• Add a listener to the selectedToggleProperty of
the ToggleGroup. This property changes whenever a
different radio button in the group is selected
• Attach ChangeListener to the ToggleGroup.
• Triggered whenever the selection changes.
Responding to
RadioButton using ToggleGroup
[Link]().addListener(new
ChangeListener<Toggle>()
{
@Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle)
{
if ([Link]() != null)
{
[Link]([Link]().getUser
Data().toString());}
}
});
This code adds a listener to a ToggleGroup to detect when the selected RadioButton
changes. When a new radio button is selected, the associated user data is retrieved and
displayed in a Label. ObservableValue is an interface in JavaFX that represents a value that
can be observed for changes
Responding to
RadioButton using ToggleGroup
• [Link]().addListener(...)
- Adds a ChangeListener to watch for changes in the selected toggle (radiobutton).

• changed(...) method
- Called automatically when the selected toggle changes.

• [Link]().getUserData()
- Gets the custom value (user data) attached to the selected radio button

• [Link](...)
- Updates the label to show which option the user selected.

• ObservableValue<? extends Toggle> ov:


- This is the property being observed ; in this case, selectedToggleProperty() of
the ToggleGroup.
CheckBox Controls (1 of 4)
• CheckBox controls allow the user to make yes/no or
on/off selections.
CheckBox Controls (2 of 4)
• The CheckBox class is in the [Link]
package.

CheckBox choice1 = new CheckBox("Choice 1");


CheckBox choice2 = new CheckBox("Choice 2");
CheckBox choice3 = new CheckBox("Choice 3");
CheckBox Controls (3 of 4)
• To determine whether a CheckBox is selected, you call
the CheckBox class’s isSelected method:

if ([Link]())
{
// Code here executes if the check
// box is selected.
}
CheckBox Controls (4 of 4)
• You can select a CheckBox in code with the CheckBox
class’s setSelected method:

[Link](true);
Responding to CheckBox Clicks
• If you want an action to take place immediately when the
user clicks a CheckBox, register an ActionEvent
handler with the CheckBox control.
• The process is the same as with the Button control.
• You can also use selectedProperty() to listen for
changes.
Responding to CheckBox Clicks
CheckBox cb1 = new CheckBox("Email Alerts");
[Link]().addListener((obs,
wasSelected, isNowSelected) -> {
if (isNowSelected) {
[Link]("Email Alerts enabled");
} else {
[Link]("Email Alerts disabled");
}
});
Responding to CheckBox Clicks
• [Link]() — gets the observable property that
tracks whether the checkbox is selected (true) or not (false).

• addListener(...) — adds a listener that reacts to changes in the


selected state.
• ChangeListener<Boolean> — this listener listens for Boolean
changes (checked/unchecked).
• isNowSelected tells whether the checkbox is nowchecked (true)
or not (false).
TextArea Controls (1 of 4)
• A TextArea is a multiline TextField that can accept
several lines of text input, or display several lines of text.
• The TextArea class (in the [Link]
package) has two constructors.
TextArea textArea = new TextArea();

TextArea textArea = new TextArea("This is the initial text.");


TextArea Controls (2 of 4)
Some of the TextArea Class Methods
Method Description
setPrefColumnCount(value) The value argument is an int. Sets the preferred number of text
columns. (The default value is 40.)
setPrefRowCount(value) The value argument is an int. Sets the preferred number of text
rows. (The default value is 10.)
setWrapText(value) The value argument is a boolean. If the argument is true, the
the TextArea will wrap a line of text that exceeds the end of a
row. If the argument is false, then the TextArea will scroll
when a line of text exceeds the end of a row. (The default value is
false. )
getText()
Returns the contents of the TextArea as a String.

setText(value) The value argument is a String. The argument becomes the


text that is displayed in the TextArea control.
TextArea Controls (3 of 4)
• Example: Creating an empty TextArea control with a
width of 20 columns and a height of 30 rows:

TextArea textArea = new TextArea();


[Link](20);
[Link](30);
TextArea Controls (4 of 4)
• By default, TextArea controls do not perform text
wrapping.
• To enable text wrapping, use the setWrapText method.
• The method accepts a boolean argument: true = text
wrapping is enabled, false = text wrapping is disabled.

[Link](true);

You might also like