RadioButton to change background color in Java

Hai Vo :

guys, I'm just trying to create 3 Radio Buttons that will change my background color in JavaFX. And I got stuck, I got this error "Symbols not found" in the updateBackGround() method, specifically the "pane1.setBackground(new...."

Java could not find the "pane1" symbol. Please help me. Thank you so much. Below is my code.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;

public class Project2 extends Application
{
   public RadioButton RedButton;
   public RadioButton BlueButton;
   public RadioButton GreenButton;

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

   public void start(Stage primaryStage)
   {
      Pane myPane1 = pane1();   
      Scene scene1 = new Scene(myPane1);    
      primaryStage.setScene(scene1);    
      primaryStage.setTitle("HAI VO");
      primaryStage.show();
   }

   public Pane pane1()
   {
     ToggleGroup group1 = new ToggleGroup();

     RedButton = new RadioButton("RED");
     RedButton.setToggleGroup(group1);
     RedButton.setOnAction(event -> updateBackGround());

     BlueButton = new RadioButton("BLUE");
     BlueButton.setToggleGroup(group1);
     BlueButton.setOnAction(event -> updateBackGround());

     BlueButton.setSelected(true);

     GreenButton = new RadioButton("Green");
     GreenButton.setToggleGroup(group1);
     GreenButton.setOnAction(event -> updateBackGround());

     GridPane pane1 = new GridPane();

     pane1.add(RedButton,5,0);
     pane1.add(GreenButton,10,0);
     pane1.add(BlueButton,20,0);

     pane1.setHgap(5);
     pane1.setVgap(5);

     pane1.setPadding(new Insets(20,20,20,20));

     updateBackGround();
     return pane1;
   } 

   public void updateBackGround()
   {
      if (RedButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFil(Color.RED,CornerRadii.EMPTY, Insets.EMPTY)));
      }

      if (BlueButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFill(Color.BLUE,CornerRadii.EMPTY, Insets.EMPTY)));
      }

      if (GreenButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFill(Color.GREEN,CornerRadii.EMPTY, Insets.EMPTY)));        
      }

   }
}
`

Stephen P :

You have both a method pane1() which returns a Pane, and within that method you have a local variable pane1 which is a GridPane.

Later, in updateBackground() you reference pane1 which, with no parentheses, looks like a reference to a class member or to a local variable — but there is neither a class member nor a variable within updateBackground named pane1.

updateBackground doesn't see the pane1 variable declared within the pane1() method. It also doesn't see the myPane1 Pane created within start(), which will only be accessible by retrieving it from scene1 that is set within setScene(scene1)

This is mainly a matter of the scoping rules of Java, and is not specific to JavaFX.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=478043&siteId=1