In this post, we are going to develop an application, that displays radio buttons on a scene and depends on the button click, respective color is set as window background.
bgColorChooserUsingRadioButton.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.BgColorChooserUsingRadioButtonController"
fx:id="vBox1" spacing="20" alignment="center">
<HBox fx:id="lableHBox">
<Label fx:id="label1" text="Click on a button to reflect the same background color" wrapText="true"/>
</HBox>
<HBox fx:id="colorChooserHBox">
<VBox>
<RadioButton fx:id="red" text="red"/>
<RadioButton fx:id="green" text="green"/>
<RadioButton fx:id="blue" text="blue"/>
</VBox>
<VBox>
<RadioButton fx:id="azure" text="azure"/>
<RadioButton fx:id="aqua" text="aqua"/>
<RadioButton fx:id="black" text="black"/>
</VBox>
<VBox>
<RadioButton fx:id="brown" text="brown"/>
<RadioButton fx:id="lightsalmon" text="lightsalmon"/>
<RadioButton fx:id="lightgreen" text="lightgreen"/>
</VBox>
</HBox>
</VBox>
BgColorChooserUsingRadioButtonController.java
package com.sample.app.widgets.controller;
import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
public class BgColorChooserUsingRadioButtonController implements Initializable {
@FXML
private VBox vBox1;
@FXML
private HBox colorChooserHBox;
@FXML
private Label label1;
@Override
public void initialize(URL location, ResourceBundle resources) {
DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(5);
dropShadow.setOffsetY(5);
dropShadow.setColor(Color.GRAY);
vBox1.setEffect(dropShadow);
colorChooserHBox.setSpacing(10);
colorChooserHBox.setPadding(new Insets(20, 20, 20, 20));
Font font = Font.font("Verdana", FontPosture.REGULAR, 25);
Iterator<Node> nodes = colorChooserHBox.getChildren().iterator();
colorChooserHBox.setSpacing(10);
ToggleGroup toggleGroup = new ToggleGroup();
while (nodes.hasNext()) {
VBox vBox = (VBox) nodes.next();
vBox.setSpacing(10);
Iterator<Node> vBoxNodes = vBox.getChildren().iterator();
while (vBoxNodes.hasNext()) {
Node node = vBoxNodes.next();
if (node instanceof RadioButton) {
RadioButton button = (RadioButton) node;
toggleGroup.getToggles().add(button);
button.setFont(font);
button.setOnAction((event) -> {
vBox1.setStyle("-fx-background-color:" + button.getText());
});
}
}
}
label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 50));
label1.setPadding(new Insets(20, 20, 20, 20));
nodes = colorChooserHBox.getChildren().iterator();
}
}
BgColorChooserUsingRadioButtonDemo.java
package com.sample.app.widgets;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class BgColorChooserUsingRadioButtonDemo extends Application {
private static final String FXML_FILE = "/bgColorChooserUsingRadioButton.fxml";
private static final String STAGE_TITLE = "Choose background color";
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));
Scene scene = new Scene(root, 1000, 600, Color.WHITE);
primaryStage.setTitle(STAGE_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
Select any radio box of your interest, same color is set as window background.
No comments:
Post a Comment