1

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)));        
      }

   }
}
`

Hai Vo
  • 31
  • 1
  • 1
  • 5
  • Does this help: https://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par – James_D Apr 06 '20 at 23:52

1 Answers1

3

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.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • I see, so the method did not recognize my variable. Do you have any suggestions on how to fix this? I tried to rename the Pane1 method into a different name but does not work so far. – Hai Vo Apr 06 '20 at 23:59
  • @HaiVo — Renaming so both the method and variable don't have the same "pane1" name is good, reduces confusion. I have looked at JavaFX but haven't used it for any real work, so may be off-base with any recommendations… I see in `pane1()` you are setting `.setOnAction(event -> updateBackGround())` — `pane1` _does_ have access to the variable, so possibly `.setOnAction(event -> updateBackGround(pane1))` and add a parameter to your method `updateBackGround(Pane p)` so inside it would be `p.setBackground(…)` (?) – Stephen P Apr 07 '20 at 00:12
  • Okay, I got it working, thank you so much for your help. Stay safe during the quarantine mate. – Hai Vo Apr 07 '20 at 00:20