In this post, we are going to develop an application, that displays toggle buttons on a scene and depends on the button click, respective color is set as window background.
Find the below working application.
bgColorChooserUsingToggleButton.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.BgColorChooserUsingToggleButtonController"
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>
<ToggleButton fx:id="red" text="red"/>
<ToggleButton fx:id="green" text="green"/>
<ToggleButton fx:id="blue" text="blue"/>
</VBox>
<VBox>
<ToggleButton fx:id="azure" text="azure"/>
<ToggleButton fx:id="aqua" text="aqua"/>
<ToggleButton fx:id="black" text="black"/>
</VBox>
<VBox>
<ToggleButton fx:id="brown" text="brown"/>
<ToggleButton fx:id="lightsalmon" text="lightsalmon"/>
<ToggleButton fx:id="lightgreen" text="lightgreen"/>
</VBox>
</HBox>
</VBox>
BgColorChooserUsingToggleButtonController.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.ToggleButton;
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 BgColorChooserUsingToggleButtonController 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);
vBox1.setStyle("-fx-background-color:white");
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 ToggleButton) {
ToggleButton button = (ToggleButton) node;
toggleGroup.getToggles().add(button);
button.setFont(font);
button.setOnAction((event) -> {
if (toggleGroup.getSelectedToggle() == null) {
vBox1.setStyle("-fx-background-color:white");
return;
}
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();
}
}
BgColorChooserUsingToggleButtonDemo.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 BgColorChooserUsingToggleButtonDemo extends Application {
private static final String FXML_FILE = "/bgColorChooserUsingToggleButton.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
Click on the button of your interest to select the background color. For example, I selected aqua button.
If you deselect aqua button by reclicking, you will see white background back.
Previous Next Home
No comments:
Post a Comment