How to call method of Button Automatically After initialization? - javafx

I have table view which i can not initialize into Initializable method of controller.
So i added that table view to method of Button Click Event.
public void acRefresh(){....}
Now i want to run this method for one time after Initialization but not in overridden Initialize method.
is there any other method aside from implementing Initializable class?

You can, for example, get the controller from the FXML loader and then you can call any method at any time after the initialization.

I used Platform.runlater into initialize method solve this problem.
Platform.runLater(new Runnable() {
#Override public void run() {
acRefresh();
}
});

Related

JavaFx with Google Guice gives two different instances of Controller

I introduced Google Guice to my JavaFx applicatiomn. However I have some problem when I try to #Inject my controller into another controller.
First of all I do:
loader.setControllerFactory(Main.getInjector()::getInstance);
MainController has TopMenuButtonsController included in fxml file:
<fx:include fx:id="topMenuButtons" source="TopMenuButtons.fxml" />
Then I try to load my MainController using FXMLLoader with Guice Controller Factory. TopMenuButtons are initialized automatically because it's included to MainController.
Finally I try to inject MainController into TopMenuButtonsController (because buttons control what to display in MainController:
public class TopMenuButtonsController {
private MainController mainController;
#Inject
public void setMainController(MainController mainController) {
this.mainController = mainController;
}
#FXML
public void onCreateOrder(ActionEvent event) {
mainController.setCenter(MainController.CREATE_ORDER_FXML);
}
It succeeds but the instance injected here has all field set to null (#FXML annotation didn't work). I also see that this instance injected here is some different one than I use (different object id)
I can simply make it work by doing this in MainController:
#FXML
private void initialize() {
topMenuButtonsController.setMainController(this);
}
but my intention was to get rid of such things and use DI. What I can do?

Null exception when I try to access a controller that I passed using JavaFX

I have a root layout with multiples Tabs. From my main App I open the root layout. There I included multiple FXMLs with their own controllers.
I am trying to pass the main controller to one of the Tabes controller.
The issue I am having, everything works as expected, but I get a null exception when I try to click on an action button from the new tab.
RootLayout FXML
<fx:indlue fx:id="myNewTabAnchorPane" source="NewTabFXML.fxml"/>
RootLayout Controller
#FXML NewTabController newTabController;
mainTabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
#override
public void change(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue){
if(newValue == myTab){
newTabController.setMyRootController(this);
}
NewTabController
public void setMyRootController(RootController rootController){
this.rootController = rootController;
System.out.println(rootController.getID); // this prints fine
}
However, if I trigger this action I get blank from the same controller
#FXML
public void createAction(ActionEvent event) throws IOException{
System.out.println(rootController.getID); // with this I get null value.
}
What am I missing?
Here is the problem:
#FXML NewTabController newTabController;
It should be myNewTabAnchorPaneController which is not a partially lowercased class name, but fx:id + Controller concatenation.

Event handling in multiple FXML

I have made this UI (see image) in FXMl. The Top.fxml, Bottom.fxml, Left.fxml, Right.fxml are put under a Main.fxml and arranged via BorderPane. The Left.fxml and Right.fxml are in SplitPane.
What I am trying to do is, when the button from Top.fxml clicked it should execute the function in Controller file. Below is the code from the controller file. I am getting java.lang.NullPointerException on list.setItems(listItems); .This also happens when I put list.setItems(listItems); in the initialize method.
#FXML
private Button btn;
#FXML
private ListView<String> list;
ObservableList<String> listItems = FXCollections.observableArrayList("A","B","C","D");
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void addToList(ActionEvent event) {
list.setItems(listItems);
}
The 'list' variable is null if the code has not been injected correctly, i.e. Top.fxml has not been loaded correctly.
Please provide the code that load the fxml file if you cannot solve the issue.

Run method from controller class on startup Javafx

I got a javafx application and I got a main where I set up my stage and launch the application. I also have a controller class:
public class Controller
{
#FXML Button button;
public void test(){
button.setText("Button");
}
}
How would I go about running the test method on startup. I know I can create an instance in the main class...
public class Main extends Application{
public void start(Stage primaryStage) throws Exception {
...
public static void main (String[] args){
launch (Main.class);
Controller cont = Controller();
cont.test();
}
}
and that would work. However it is not ideal for me. I was wondering if it was possible to run the method directly from the controller class, if not is there a better way of handling this? Thanks
The initialize() method is called automatically when the FXML is loaded:
public class Controller
{
#FXML Button button;
public void initialize(){
button.setText("Button");
}
}
Note that your code in the Main class won't work at all. First, launch() does not exit until you exit the application, and second, you are calling it on a new instance of the controller, not the one that is connected to the UI you load from the FXML file.

I am looking for fxml update on startup

In javafx there are onclickaction , ondragaction, etc
But I cant find something like onstartupaction that perform an action when the application starts
See the Application javadoc for an instance of the application lifecycle. When your application starts, it's start method will be called.
Initialization of fxml is different from application startup, as one application may have many fxml documents loaded many times, each time having a new controller instantiatd and it's initialization method called. This is described in the #FXML Controllers section of the Introduction to FXML documentation.
In the following controller, the initialize method will be invoked by the FXMLLoader. Every time it loads an FXML document referencing the controller class, the loader will create a new controller instance and invoke initialize on it.
public class MyController implements Initializable {
#FXML private Button button;
#FXML
protected void initialize()
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("You clicked me!");
}
});
}
}

Resources