JavaFX RadioButton change color for Text

Description

JavaFX RadioButton change color for Text

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;

public class Main extends Application {
  @Override/*from  w ww  . j  av a  2 s.  c o  m*/
  public void start(Stage primaryStage) {
    RadioButton rbRed = new RadioButton("Red");
    RadioButton rbYellow = new RadioButton("Yellow");
    RadioButton rbBlack = new RadioButton("Black");
    RadioButton rbOrange = new RadioButton("Orange");
    RadioButton rbGreen = new RadioButton("Green");

    ToggleGroup tg = new ToggleGroup();
    rbRed.setToggleGroup(tg);
    rbYellow.setToggleGroup(tg);
    rbBlack.setToggleGroup(tg);
    rbOrange.setToggleGroup(tg);
    rbGreen.setToggleGroup(tg);

    HBox rbPane = new HBox(10);
    rbPane.getChildren().addAll(rbRed, rbYellow, rbBlack, rbOrange, rbGreen);

    Text message = new Text(50, 50, "Programming is fun");
    message.setFont(Font.font("SansSerif", FontWeight.BOLD, 20));

    Pane messagePane = new Pane();
    messagePane.getChildren().add(message);
    messagePane.setStyle("-fx-border-color: black");

    Button btLeft = new Button("<=");
    Button btRight = new Button("=>");

    HBox arrowPane = new HBox(10);
    arrowPane.getChildren().addAll(btLeft, btRight);

    BorderPane pane = new BorderPane();
    pane.setTop(rbPane);
    pane.setBottom(arrowPane);
    pane.setCenter(messagePane);
    rbPane.setAlignment(Pos.CENTER);
    arrowPane.setAlignment(Pos.CENTER);

    rbRed.setOnAction(e -> message.setFill(Color.RED));
    rbYellow.setOnAction(e -> message.setFill(Color.YELLOW));
    rbBlack.setOnAction(e -> message.setFill(Color.BLACK));
    rbOrange.setOnAction(e -> message.setFill(Color.ORANGE));
    rbGreen.setOnAction(e -> message.setFill(Color.GREEN));

    btLeft.setOnAction(e -> message.setX(message.getX() - 10));
    btRight.setOnAction(e -> message.setX(message.getX() + 10));

    Scene scene = new Scene(pane, 400, 150);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}



PreviousNext

Related