1
votes

Is it possible to edit/access javaFX elements (such as Buttons, Labels, etc.) in java code which is NOT the controller class?

I have written an entire program in java and want to make a GUI for it in javaFX (using Scene Builder). Since the entire code ALREADY is written in Java classes outside the FXML-controller class, is it possible to access elements like "Labels" inside my already written classes?

All I want to do is use this code outside the FXML-controller class: label1.setText("Something");

So it updates on the GUI.

If not, it is a very time-consuming process to implement my java code in the FXML-controller class.

1
Call class from FXML and pass the elements instance as constructor argument? - Jaskaranbir Singh
Have you written all of the GUI logic without using JavaFX? If so, then your going to have to re-write that to use JavaFX. If it's just your business logic, then there's no reason you can't create a wrapper for it that translates between JavaFX, and non FX code. - Frank
I am not sure to understand. You can put accessors like Label getLabel() in your controller to access it elsewhere. Or you can call other classes methods from your controller by passing your components as parameters. - Kwoinkwoin
Frank that is correct understanding of my problem. I actually have not learned about wrapper yet. But I will do that asap. - C. Skov
I don't really understand the problem. Can't you just pass a reference to an instance of your existing class to the controller, and access it from the controller that way? Can you post an example to clarify what you mean? - James_D

1 Answers

0
votes

So I think your solution depends on how safe you need your information to be when interfacing with the FX GUI. If you are OK providing your data directly to the controller, then I'd suggest simply passing your data object into the controller's constructor and storing a reference to it locally. You can then use any getters/setters from the data object to populate your GUI, or update the model.

 public class Controller {
      private Object dataObject;

      public Controller(Object dataObject) {
           this.dataObject = dataObject;
      }
 }

However, if you want to protect your data, and prevent the GUI from directly modifying or interacting with it - you'll need to use a wrapper that protects your information.

 public class Wrapper {
      private Object dataObject;

      public Controller(Object dataObject) {
                this.dataObject = dataObject;
      }

      // allowable function call
      public Object functionOne() {
           return dataObject.functionOne();
      }
 }

You only need to implement functions in the wrapper that are directly needed by the GUI, thus preventing calls into your data object that you think should be restricted from the GUI.

Your controller then becomes:

 public class Controller {
      private Wrapper dataObject;

      public Controller(Wrapper dataObject) {
           this.dataObject = dataObject;
      }
 }