-1

lets say you have lable in MainWindow ..

and you want change value of this lable from Window2

Not in Same Window !!

i want the changes while the MainWindow its open

  • 3
    Hi, welcome to StackOverflow. Please think about how you would explain this problem to a co-worker, and edit the question accordingly. At present we don't know your use case, and we can't see your code; so we have nothing to go on. – MandyShaw Nov 03 '18 at 17:45
  • 1
    See answer here: [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/51050736#51050736) – Zephyr Nov 04 '18 at 14:42
  • 1
    Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – Zephyr Nov 04 '18 at 14:42

1 Answers1

-1

It's easy if ur using 2 .fxml files with there own controllers if that's the case create a new class file namely 'AllControllers'

you have two 2 controllers namely ControllerWindow1 and ControllerWindow2

public class AllControllers {


     private static ControllerWindow1 control1;
     private static ControllerWindow2 control2;


    public static ControllerWindow1 getControl1() {
        return control1;
    }
    public static void setControl1(ControllerWindow1 control1) {
        Controlls.control1 = control1;
    }
    public static ControllerWindow2 getControl2() {
        return control2;
    }
    public static void setControl2(ControllerWindow2 control2) {
        Controlls.control2 = control2;
    }



}

You have to initialize each controller like this

public class ControllerWindow1 implements Initializable{

    @FXML 
    public Label mylabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        AllControllers.setControl1(this);
    }

}

Now you can access your controller from any class. Just use

AllControllers.getControl1().mylabel.setText("hello");
Ash
  • 71
  • 9
  • from where you get Controlls – Hellooworld Nov 04 '18 at 12:42
  • You have to have a controller for every FXML file. if ur not using FXML files then just use the class name instead. – Ash Nov 04 '18 at 13:21
  • I'm afraid this is a very bad idea. Using a Singleton model for accessing controllers is not ideal. Instead, just pass the controller references to the controllers that need it... – Zephyr Nov 04 '18 at 14:43
  • I'm pretty new to JavaFX I said whatever worked for me. how can we pass controller references to another controller directly? I have a label in controller1 – Ash Nov 04 '18 at 15:51
  • @Zephyr I want to use it in Controller2 so if I do, new Controller2 (this) in an Initializable method this might create an object?? – Ash Nov 04 '18 at 15:59
  • See my answer to [this question](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/51050736#51050736) regarding the same thing. – Zephyr Nov 04 '18 at 16:03
  • @Zephyr oh I see now I can use anything of controller1 in controller2 but i also wanna use anything of controller 2 in controller1 and if i use new controller1(this); this gonna create 2 controller1 windows – Ash Nov 04 '18 at 16:27
  • 1
    So just create a method in `controller1` that accepts a reference to `controller2`. Not sure what else you mean. – Zephyr Nov 04 '18 at 16:48
  • @Zephyr thanks your clarification now I understood. Just 1 more question if I have a couple of controllers and i want them to have access to one another. let's say 5. Should I pass reference same way? it sure gonna mess up the code a little like a spider web each controller gonna have 4 methods just to set reference. In that case, should I use my way? or should I pass reference 1 by 1 for better performance :o – Ash Nov 04 '18 at 17:52
  • 1
    @Ash - Honestly, I would rethink your design if that were the case. You should not need access to so many controllers at once. You may have need to share **data** between controllers, but not the entire controllers... – Zephyr Nov 05 '18 at 04:44