Answer the question
In order to leave comments, you need to log in
Handling events from a button?
I'm making a javafx application using scene builder
. I want to make a simple button click event handler.
fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-1.0" maxWidth="-1.0" minHeight="-1.0" minWidth="-1.0" prefHeight="330.0" prefWidth="443.99980000000505" styleClass="background" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<children>
<VBox id="VBox" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="330.0" prefWidth="443.99980000000505" spacing="5.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label id="welcomeLabel" disable="false" prefHeight="35.0" prefWidth="201.0" text="WELCOME" textAlignment="CENTER" textOverrun="CLIP" underline="false" wrapText="false">
<font>
<Font size="40.0" />
</font>
<textFill>
<Color blue="1.000" green="0.000" red="0.333" fx:id="x2" />
</textFill>
</Label>
<Label id="userNameLabel" text="User name" textFill="#5200ff">
<font>
<Font size="18.0" fx:id="x1" />
</font>
</Label>
<TextField id="userNameTxtF" alignment="CENTER" prefWidth="206.0" />
<Label id="passwordLabel" font="$x1" text="Password" textFill="$x2" />
<PasswordField id="passPField" alignment="CENTER" prefWidth="206.0" />
<Button id="signInBtn" defaultButton="true" mnemonicParsing="false" onAction="#action1" text="Sign In" textFill="$x2">
<font>
<Font size="30.0" />
</font>
</Button>
</children>
</VBox>
</children>
<stylesheets>
<URL value="@../res/DarkTheme.css" />
</stylesheets>
</AnchorPane>
public class Controller {
private String user;
private String pass;
@FXML
private Label welcomeLabel;
@FXML
private Label userNameLabel;
@FXML
private Label passwordLabel;
@FXML
private TextField userNameTxtF;
@FXML
private PasswordField passPField;
@FXML
private Button signInBtn;
public Controller(){
user = "user";
pass = "12345678";
}
@FXML
private void handleButtonAction(ActionEvent event) {
signInBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEvent -> {
System.out.println("xcvbn");
});
}
@FXML
private void action(){
signInBtn.setOnAction((event)->{
if(userNameTxtF.getText() == user && passPField.getText() == pass){
welcomeLabel.setText("ALL'S OK");
}
else System.out.println("no");
});
}
@FXML
private void action1(){
signInBtn.setText("fgsdhjk");
}
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
if(userNameTxtF.getText() == user && passPField.getText() == pass)
System.out.println("fhbskf");
}
}
onAction="#"
button parameter. And in any case it gives the same error:Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/374958851.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
... 47 more
Caused by: java.lang.NullPointerException
at sample.Controller.action1(Controller.java:69)
... 57 more
Answer the question
In order to leave comments, you need to log in
See the lowest of the Exceptions:
Caused by: java.lang.NullPointerException
at sample.Controller.action1(Controller.java:69)
... 57 more
@FXML
private Button signInBtn;
<Button id="signInBtn" defaultButton="true" mnemonicParsing="false" onAction="#action1" text="Sign In" textFill="$x2">
<font>
<Font size="30.0" />
</font>
</Button>
<Button fx:id="signInBtn" defaultButton="true" mnemonicParsing="false" onAction="#action1" text="Sign In" textFill="$x2">
<font>
<Font size="30.0" />
</font>
</Button>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question