JavaFX Basic Events
In JavaFX, events are represented by objects. When an event occurs, system collects all the information relevant to the event and construct an object …
If you are going to draw something on the screen and handle some mouse event, then you can only use MouseEvent listener and AnimationTimer that’s it!! But in one way or another, most probably you will need to add some inputs for user’s interactivity. For that you may want to use Checkbox, RadioButton, TextField and more … Let’s look at the some of these objects:
These two components display text to the user. User can view but not edit.
Actually objects that is part of the user interface but not editable extends object called Labeled
. For instance:
public class Label extends Labeled {}
public class Button extends ButtonBase{}
public abstract class ButtonBase extends Labeled{}
Labeled
class defines the following methods:
setGraphic(node)
: sets the control’s graphical element. For instance: we can use this method when we want to add image to the buttonsetText(string)
: sets the text that is displayed on the control. Text can be also multi-line using the new line character "\n"
setFont(font)
: sets the font for the textsetTextFill(color)
: sets the paint that is used for drawing the text.setGraphicTextGap(size)
: sets the amount of space between the text and the graphic.setContentDisplay(displayCode)
: set where the graphic should be places with respect to the next. Examples: ContentDisplay.LEFT, ContentDisplay.RIGHT, ContentDisplay.TOP
Label onlyText = new Label("Hello");
Label textWithIcon = new Label("Choose Icon", myIconObject);
message.onlyText("-fx-border-color: green; -fx-border-width: 12px; " +
"-fx-background-color: white; -fx-padding: 3px");
Label
, it displays text to the user and user can click the button as well.Button onlyText = new Button("Text");
Button buttonWithIcon = new Button("Another Button", myIconObject);
ActionEvent
is generated. We can register an event handler for the button’s action using setOnAction
method:buttonWithIcon.setOnAction( e -> System.out.println("...") );
setDisable(boolean)
and setToolTip(string)
button.setDefaultButton(true);
Labeled
CheckBox test = new CheckBox("Check Test");
test.isSelected()
(returns true is selected otherwise false)test.setSelected(boolean)
isSelected()
method will return the state. If we want to also listen the state changes in the checkbox we can register a listener with test.setOnAction(e -> ...)
Note:
setSelected()
method doesn’t trigger an action. If we want to simulate user event, then we can callfire()
method.
ToggleGroup
.
ToggleGroup
is not component and can be visible as itself on the screenToggleGroup radiouGroup = new ToggleGroup();
RadioButton redButton = new RadiouButton("Red");
redButton.setToggleGroup(radiouGroup);
RadioButton blueButton = new RadiouButton("Blue");
blueButton.setToggleGroup(radiouGroup);
RadioButton greenButton = new RadiouButton("Green");
greenButton.setToggleGroup(radiouGroup);
// initiality redButton is selected
radiouGroup.selectToggle(redButton);
If we want to learn selected radioButton:
Toggle selection = radiouGroup.getSelectedToggle();
if (selection == redButton) {}
else if (selection == blueButton) {}
...
setText(text)
& getText()
appendText(text)
setFont(font)
setEditable(boolean)
: if we set to false then user can’t modify the inputsinput.setPrefColumnCount(n)
setPrefColumnCount(n)
& setPrefRowCount(n)
setWrapText(boolean)
(in default it is false) to wrap longer text to the next lineLabeled
.Label
, Button displays text to the user and user can click the button as well.test.isSelected()
ToggleGroup
which is not component and can be visible as itself on the screen. ToggleGroup is used for grouping the RadioButtonsLast but not least wait for the next one …
In JavaFX, events are represented by objects. When an event occurs, system collects all the information relevant to the event and construct an object …
The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the screen just …