So, I was making a menu in FXML and I was using a Group so I had to set a prefWidth otherwise it'd look weird. To do that, I wanted to initialize a variable with the screen width in the controller, and then use the width variable while setting up the menu in FXML. But I just couldn't find a way to do that.
Generalizing this problem, I want to initialize a variable in a controller and then use it in FXML like this:
[Controller]
package sample;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@Override
public void initialize (URL url, ResourceBundle resourceBundle) {
String text = "test";
}
}
[FXML]
<?import javafx.scene.control.Label?>
<?import javafx.scene.BorderPane?>
<BorderPane>
<center>
<label text="<!--use var 'text' here-->"/>
</center>
</BorderPane>
I know, there are other ways to do it (like id-ing it and setting the text in the controller) but I just wanna see if it is possible to do it this way.