I'm having some trouble programming the controller for a fxml file in javafx. Here's my fxml code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="448.0" prefWidth="657.0" style="-fx-background-color: #8B0000;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="conecta4.loginController">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints maxWidth="411.0" minWidth="289.0" prefWidth="397.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="383.0" minWidth="10.0" prefWidth="260.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane prefHeight="522.0" prefWidth="490.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="38.0" prefWidth="341.0" text="Iniciar sesión" textFill="WHITE">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</GridPane>
<Button mnemonicParsing="false" onAction="#changeScreenButtonPushed" style="-fx-background-color: #8B0000; -fx-border-color: #8B0000; -fx-text-fill: #00FFFF;" text="Registro" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="user" promptText="Usuario">
<GridPane.margin>
<Insets left="10.0" right="20.0" />
</GridPane.margin>
</TextField>
<PasswordField promptText="Contraseña" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" right="20.0" />
</GridPane.margin>
</PasswordField>
<Button mnemonicParsing="false" onAction="#doesPlayerExist" style="-fx-background-color: #8B0000;" text="Continuar" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</GridPane>
<GridPane style="-fx-background-color: #FFFFFF;" GridPane.columnIndex="1" GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="playerNameLabel" alignment="CENTER" prefHeight="43.0" prefWidth="340.0" style="-fx-text-fill: #8B0000;" text="Conecta 4" textFill="#f8f7f7" GridPane.halignment="CENTER" GridPane.rowIndex="1">
<font>
<Font name="System Bold" size="29.0" />
</font>
</Label>
<Label fx:id="msg" text="Label" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</GridPane>
And this is my controller:
package conecta4;
import DBAccess.Connect4DAOException;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import model.Connect4;
/**
*
* @author Javi
*/
public class loginController implements Initializable {
Connect4 connect4;
@FXML
private Label msg;
@FXML
private TextField user;
/*Cuando se llame a este metodo cambia de escena a resgistro*/
@FXML
public void changeScreenButtonPushed(ActionEvent event) throws IOException{
Parent conecta4Parent = FXMLLoader.load(getClass().getResource("Registro.fxml"));
Scene conecta4Scene = new Scene(conecta4Parent);
// Esta linea recibe la informacion de Stage
Stage ventana;
ventana = (Stage)((Node)event.getSource()).getScene().getWindow();
ventana.setScene(conecta4Scene);
ventana.show();
}
/**
*
* @param event
*/
@FXML
public void doesPlayerExist(ActionEvent event, Connect4 objeto, Label msg, TextField user){
if( objeto.exitsNickName(user.toString())==false){
msg.setText("Este user no está registrado");
}else{
msg.setText("lsdjfl");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
connect4 = Connect4.getSingletonConnect4();
} catch (Connect4DAOException ex) {
Logger.getLogger(loginController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
exitsNickname() is correctly imported from a local library. The problem is, it doesn't find doesPlayerExist() button handler, but changeScreenButtonPushed() works just fine, so I think the fxml and the controller are linked correctly. Thanks for your help, I really don't know what to do.
This is the error log:
Exception in Application start method
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:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#doesPlayerExist', either the event handler is not in the Namespace or there is an error in the script.
file:xxx
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at conecta4.Conecta4.start(Conecta4.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
... 1 more
Exception running application conecta4.Conecta4
Java Result: 1