Issue
I am testing some stuff out with JavaFX and came to a problem. I have 3 radio buttons that have an action event. These radio buttons each represent a color (red, blue and yellow) that if you press it, a square should change the color.
Below is my pane with the buttons: public class ButtonsPane extends VBox { private final ControlPane controlPane;
public ButtonsPane(ControlPane controlPane) {
this.controlPane = controlPane;
this.setPrefHeight(150);
this.setPrefWidth(120);
RadioButton radioButton1 = new RadioButton("Red");
radioButton1.setSelected(true);
radioButton1.setOnAction(e -> controlPane.setColor("Red"));
RadioButton radioButton2 = new RadioButton("Blue");
radioButton2.setOnAction(e -> controlPane.setColor("Blue"));
RadioButton radioButton3 = new RadioButton("Yellow");
radioButton3.setOnAction(e -> controlPane.setColor("Yellow"));
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().addAll(radioButton1, radioButton2, radioButton3);
this.getChildren().addAll(radioButton1, radioButton2, radioButton3);
this.setPadding(new Insets(20, 0, 0, 15));
}
}
Below is is my controlpane. The action event calls a method in DrawPane that tries to convert the String color to a real Color object value.
public class ControlPane extends VBox {
private Color color;
private final DrawPane drawPane;
public ControlPane(boolean buttons) {
ButtonsPane buttonsPane = new ButtonsPane(this);
ListViewPane listViewPane = new ListViewPane();
drawPane = new DrawPane();
SliderPane sliderPane = new SliderPane();
if(buttons) {
this.getChildren().add(buttonsPane);
} else {
this.getChildren().add(listViewPane);
}
this.setPrefHeight(350);
this.setPrefWidth(120);
this.getChildren().add(sliderPane);
this.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
}
public void setColor(String color) {
try {
drawPane.setColor(Color.valueOf(color));
} catch (Exception e) {
drawPane.setColor(Color.WHITE);
}
}
}
If the color fails or not there will be a method called in my drawpane that should change the color.
public class DrawPane extends BorderPane {
private final Rectangle rectangle;
public DrawPane() {
this.rectangle = new Rectangle();
this.rectangle.setFill(Color.RED);
this.rectangle.setHeight(180);
this.rectangle.setWidth(180);
this.rectangle.setStroke(Color.BLACK);
this.setCenter(this.rectangle);
this.setPadding(new Insets(10));
this.setPrefHeight(350);
this.setPrefWidth(380);
}
public void setColor(Color color) {
System.out.println("true");
this.rectangle.setFill(Color.BLACK);
this.setBackground(new Background(new BackgroundFill(Color.YELLOW, null, null)));
}
}
Scene:
public class MyScene extends Scene {
public MyScene(HBox root) {
super(root);
root.getChildren().addAll(new DrawPane(), new ControlPane(true));
}
}
The method is called as I have tested this with a System.out, but the color does not update. What am I missing or doing wrong?
Solution
Working sample
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class RectangleApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Rectangle rectangle = new Rectangle(100, 100, Color.BLACK);
StackPane stackPane = new StackPane(rectangle);
RadioButton redButton = new RadioButton("Red");
RadioButton blueButton = new RadioButton("Blue");
//It's not good style to get the color using valueOf() but I'm staying in context of the sample
redButton.setOnAction(event -> rectangle.setFill(Color.valueOf("red")));
blueButton.setOnAction(event -> rectangle.setFill(Color.valueOf("blue")));
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().setAll(redButton, blueButton);
HBox hBox = new HBox(redButton, blueButton);
VBox vBox = new VBox(hBox, stackPane);
Scene scene = new Scene(vBox, 400, 400);
stage.setScene(scene);
stage.show();
}
}
Answered By - Przemek Krysztofiak