JavaFX how to handle ActionEvent from controller class?
Hello i have problem with handling action from my button: here's my code:
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
my controller class
public class Controller {
private HelloWorld helloWorld;
private Model model;
public Controller(HelloWorld helloWorld, Model model) throws Exception {
this.helloWorld = helloWorld;
this.model = model;
System.out.println(this.mainView.returnOne());
this.helloWorld.setButtonOnAction(e->
{
System.out.println("CATCH");
});
}
}
main runClass:
public class runExample {
public static void main(String args) throws Exception {
HelloWorld helloWorld = new HelloWorld();
Model model = new Model();
Application.launch(helloWorld.getClass(),args);
Controller controller = new Controller(helloWorld, model);
}
}
`
Does anyone know why setButtonOnAction don't work in controller class but in HelloWorld class it work perfectly ? Compiler don't give me any error. Only if i switch in run class like that:
Controller controller = new Controller(mainView, model);
Application.launch(mainView.getClass(),args);
it gives me
Exception in thread "main" java.lang.NullPointerException
and if i'm using setButtonOnAction in HelloWorld class it works fine. Can u help me catch event in my controller class ? I'm using jdk8 but on 11 it isn't work too.
java user-interface events javafx event-handling
add a comment |
Hello i have problem with handling action from my button: here's my code:
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
my controller class
public class Controller {
private HelloWorld helloWorld;
private Model model;
public Controller(HelloWorld helloWorld, Model model) throws Exception {
this.helloWorld = helloWorld;
this.model = model;
System.out.println(this.mainView.returnOne());
this.helloWorld.setButtonOnAction(e->
{
System.out.println("CATCH");
});
}
}
main runClass:
public class runExample {
public static void main(String args) throws Exception {
HelloWorld helloWorld = new HelloWorld();
Model model = new Model();
Application.launch(helloWorld.getClass(),args);
Controller controller = new Controller(helloWorld, model);
}
}
`
Does anyone know why setButtonOnAction don't work in controller class but in HelloWorld class it work perfectly ? Compiler don't give me any error. Only if i switch in run class like that:
Controller controller = new Controller(mainView, model);
Application.launch(mainView.getClass(),args);
it gives me
Exception in thread "main" java.lang.NullPointerException
and if i'm using setButtonOnAction in HelloWorld class it works fine. Can u help me catch event in my controller class ? I'm using jdk8 but on 11 it isn't work too.
java user-interface events javafx event-handling
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
TheHelloWorldinstance you create is different to the oneApplication.launchuses andApplication.launchdoes not return until the javafx toolkit exits, in this case after the last window is closed...
– fabian
Nov 20 '18 at 22:39
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46
add a comment |
Hello i have problem with handling action from my button: here's my code:
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
my controller class
public class Controller {
private HelloWorld helloWorld;
private Model model;
public Controller(HelloWorld helloWorld, Model model) throws Exception {
this.helloWorld = helloWorld;
this.model = model;
System.out.println(this.mainView.returnOne());
this.helloWorld.setButtonOnAction(e->
{
System.out.println("CATCH");
});
}
}
main runClass:
public class runExample {
public static void main(String args) throws Exception {
HelloWorld helloWorld = new HelloWorld();
Model model = new Model();
Application.launch(helloWorld.getClass(),args);
Controller controller = new Controller(helloWorld, model);
}
}
`
Does anyone know why setButtonOnAction don't work in controller class but in HelloWorld class it work perfectly ? Compiler don't give me any error. Only if i switch in run class like that:
Controller controller = new Controller(mainView, model);
Application.launch(mainView.getClass(),args);
it gives me
Exception in thread "main" java.lang.NullPointerException
and if i'm using setButtonOnAction in HelloWorld class it works fine. Can u help me catch event in my controller class ? I'm using jdk8 but on 11 it isn't work too.
java user-interface events javafx event-handling
Hello i have problem with handling action from my button: here's my code:
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
my controller class
public class Controller {
private HelloWorld helloWorld;
private Model model;
public Controller(HelloWorld helloWorld, Model model) throws Exception {
this.helloWorld = helloWorld;
this.model = model;
System.out.println(this.mainView.returnOne());
this.helloWorld.setButtonOnAction(e->
{
System.out.println("CATCH");
});
}
}
main runClass:
public class runExample {
public static void main(String args) throws Exception {
HelloWorld helloWorld = new HelloWorld();
Model model = new Model();
Application.launch(helloWorld.getClass(),args);
Controller controller = new Controller(helloWorld, model);
}
}
`
Does anyone know why setButtonOnAction don't work in controller class but in HelloWorld class it work perfectly ? Compiler don't give me any error. Only if i switch in run class like that:
Controller controller = new Controller(mainView, model);
Application.launch(mainView.getClass(),args);
it gives me
Exception in thread "main" java.lang.NullPointerException
and if i'm using setButtonOnAction in HelloWorld class it works fine. Can u help me catch event in my controller class ? I'm using jdk8 but on 11 it isn't work too.
java user-interface events javafx event-handling
java user-interface events javafx event-handling
asked Nov 20 '18 at 21:30
infinf
32
32
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
TheHelloWorldinstance you create is different to the oneApplication.launchuses andApplication.launchdoes not return until the javafx toolkit exits, in this case after the last window is closed...
– fabian
Nov 20 '18 at 22:39
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46
add a comment |
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
TheHelloWorldinstance you create is different to the oneApplication.launchuses andApplication.launchdoes not return until the javafx toolkit exits, in this case after the last window is closed...
– fabian
Nov 20 '18 at 22:39
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
The
HelloWorld instance you create is different to the one Application.launch uses and Application.launch does not return until the javafx toolkit exits, in this case after the last window is closed...– fabian
Nov 20 '18 at 22:39
The
HelloWorld instance you create is different to the one Application.launch uses and Application.launch does not return until the javafx toolkit exits, in this case after the last window is closed...– fabian
Nov 20 '18 at 22:39
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46
add a comment |
1 Answer
1
active
oldest
votes
Working example ;)
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
Controller controller = new Controller(this,new Model()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
And run class:
public class runExample {
public static void main(String args) throws Exception {
Application.launch(HelloWorld.class,args);
}
}
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%2f53401850%2fjavafx-how-to-handle-actionevent-from-controller-class%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
Working example ;)
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
Controller controller = new Controller(this,new Model()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
And run class:
public class runExample {
public static void main(String args) throws Exception {
Application.launch(HelloWorld.class,args);
}
}
add a comment |
Working example ;)
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
Controller controller = new Controller(this,new Model()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
And run class:
public class runExample {
public static void main(String args) throws Exception {
Application.launch(HelloWorld.class,args);
}
}
add a comment |
Working example ;)
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
Controller controller = new Controller(this,new Model()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
And run class:
public class runExample {
public static void main(String args) throws Exception {
Application.launch(HelloWorld.class,args);
}
}
Working example ;)
public class HelloWorld extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
Controller controller = new Controller(this,new Model()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setButtonOnAction(EventHandler<ActionEvent> acn)
{
btn.setOnAction(acn);
}
}
And run class:
public class runExample {
public static void main(String args) throws Exception {
Application.launch(HelloWorld.class,args);
}
}
answered Nov 20 '18 at 22:59
infinf
32
32
add a comment |
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.
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%2f53401850%2fjavafx-how-to-handle-actionevent-from-controller-class%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
Take the time to go though a basic tutorial. docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
– Sedrick
Nov 20 '18 at 21:57
The
HelloWorldinstance you create is different to the oneApplication.launchuses andApplication.launchdoes not return until the javafx toolkit exits, in this case after the last window is closed...– fabian
Nov 20 '18 at 22:39
i fixed it already thanks.
– inf
Nov 20 '18 at 22:46