Java FXML Application: unsuccessful calling method in controller class from external class
Working on a Java FXML application called EVIF VideoRouter. Main class is then VideoRouter.java and the controller class is called VideoRouterFXMLController.java.
I have a method in the VideoRouterFXMLController.java class that I need to be able to call from other external classes within the application. Based on extensive searching and reading, I altered the FXML loader code in the main VideoRouter.java class to be thus:
FXMLLoader EVIFloader = new FXMLLoader(this.getClass().getResource("VideoRouterFXML.fxml"));
Parent EVIFroot = (Parent) EVIFloader.load();
VideoRouterFXMLController EVIFcontroller = EVIFloader.getController();
Now, as I understand this process, I'm somehow supposed to create a getter in the main VideoRouter.java class that returns the EVIFcontroller instance I've created above? And then I can use that getter in other classes and call the method inside the returned instance?
If so, I'm having difficulty figuring out where exactly to place the getter. Does it go in the controller class, or the main class? In either case it doesn't seem to be able to see the controller instance in order to return it.
Again, I've searched extensively and the related posts I've found to not appear to address this specific problem I'm having. Any help?
Many thanks in advance!
javafx fxml
add a comment |
Working on a Java FXML application called EVIF VideoRouter. Main class is then VideoRouter.java and the controller class is called VideoRouterFXMLController.java.
I have a method in the VideoRouterFXMLController.java class that I need to be able to call from other external classes within the application. Based on extensive searching and reading, I altered the FXML loader code in the main VideoRouter.java class to be thus:
FXMLLoader EVIFloader = new FXMLLoader(this.getClass().getResource("VideoRouterFXML.fxml"));
Parent EVIFroot = (Parent) EVIFloader.load();
VideoRouterFXMLController EVIFcontroller = EVIFloader.getController();
Now, as I understand this process, I'm somehow supposed to create a getter in the main VideoRouter.java class that returns the EVIFcontroller instance I've created above? And then I can use that getter in other classes and call the method inside the returned instance?
If so, I'm having difficulty figuring out where exactly to place the getter. Does it go in the controller class, or the main class? In either case it doesn't seem to be able to see the controller instance in order to return it.
Again, I've searched extensively and the related posts I've found to not appear to address this specific problem I'm having. Any help?
Many thanks in advance!
javafx fxml
add a comment |
Working on a Java FXML application called EVIF VideoRouter. Main class is then VideoRouter.java and the controller class is called VideoRouterFXMLController.java.
I have a method in the VideoRouterFXMLController.java class that I need to be able to call from other external classes within the application. Based on extensive searching and reading, I altered the FXML loader code in the main VideoRouter.java class to be thus:
FXMLLoader EVIFloader = new FXMLLoader(this.getClass().getResource("VideoRouterFXML.fxml"));
Parent EVIFroot = (Parent) EVIFloader.load();
VideoRouterFXMLController EVIFcontroller = EVIFloader.getController();
Now, as I understand this process, I'm somehow supposed to create a getter in the main VideoRouter.java class that returns the EVIFcontroller instance I've created above? And then I can use that getter in other classes and call the method inside the returned instance?
If so, I'm having difficulty figuring out where exactly to place the getter. Does it go in the controller class, or the main class? In either case it doesn't seem to be able to see the controller instance in order to return it.
Again, I've searched extensively and the related posts I've found to not appear to address this specific problem I'm having. Any help?
Many thanks in advance!
javafx fxml
Working on a Java FXML application called EVIF VideoRouter. Main class is then VideoRouter.java and the controller class is called VideoRouterFXMLController.java.
I have a method in the VideoRouterFXMLController.java class that I need to be able to call from other external classes within the application. Based on extensive searching and reading, I altered the FXML loader code in the main VideoRouter.java class to be thus:
FXMLLoader EVIFloader = new FXMLLoader(this.getClass().getResource("VideoRouterFXML.fxml"));
Parent EVIFroot = (Parent) EVIFloader.load();
VideoRouterFXMLController EVIFcontroller = EVIFloader.getController();
Now, as I understand this process, I'm somehow supposed to create a getter in the main VideoRouter.java class that returns the EVIFcontroller instance I've created above? And then I can use that getter in other classes and call the method inside the returned instance?
If so, I'm having difficulty figuring out where exactly to place the getter. Does it go in the controller class, or the main class? In either case it doesn't seem to be able to see the controller instance in order to return it.
Again, I've searched extensively and the related posts I've found to not appear to address this specific problem I'm having. Any help?
Many thanks in advance!
javafx fxml
javafx fxml
asked Nov 14 '18 at 16:00
Mark Dyson
17010
17010
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.
Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304246%2fjava-fxml-application-unsuccessful-calling-method-in-controller-class-from-exte%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.
Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
add a comment |
You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.
Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
add a comment |
You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.
Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.
You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.
Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.
answered Nov 14 '18 at 16:24
Gnas
49829
49829
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
add a comment |
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
– Mark Dyson
Nov 14 '18 at 16:42
1
1
Can you check whether your method call (
EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?– Gnas
Nov 14 '18 at 17:02
Can you check whether your method call (
EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?– Gnas
Nov 14 '18 at 17:02
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
– Mark Dyson
Nov 14 '18 at 17:20
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!
– Mark Dyson
Nov 14 '18 at 17:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304246%2fjava-fxml-application-unsuccessful-calling-method-in-controller-class-from-exte%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown