Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx, Problems with @FXML

Tags:

javafx

fxml

I'm new in java, so I have to ask you for help :P. I'm creating very simple program, but I stucked on verifying password and login textField. I'm wondering what is wrong with that code, can you help me?

Controller

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;

import java.awt.*;

public class Controller {
    @FXML
    public TextField login,password;
    public void LoginButtonClicked(ActionEvent event)
    {
    if(login.getText().equals("yes")&&password.getText().equals("yes"))
        {
            System.out.print("Congratulations");
        }
    else
        {
            System.out.print("no");
        }

    }


}

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="Profile">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Your Profile" />
                        <MenuItem mnemonicParsing="false" text="Log out" />
                        <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About program" />
                        <MenuItem mnemonicParsing="false" text="Author" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <HBox spacing="8.0">
               <children>
                  <TextField fx:id="login" promptText="User" />               // ERROR
                  <TextField fx:id="password" layoutX="10.0" layoutY="10.0" promptText="Password" />                                      //ERROR
                  <Region HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#LoginButtonClicked" text="Log In" />
                  <Button layoutX="329.0" layoutY="15.0" mnemonicParsing="false" text="Clear" />
               </children>
               <padding>
                  <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
               </padding>
            </HBox>
         </children>
      </VBox>
   </top>
   <center>
      <Label alignment="CENTER" prefHeight="130.0" prefWidth="469.0" text="Log in, in order to use this program" BorderPane.alignment="CENTER">
         <font>
            <Font size="25.0" />
         </font>
      </Label>
   </center>
</BorderPane>

And the error code

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$1/868693306.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Michael/IdeaProjects/Endomondo/out/production/Endomondo/sample/sample.fxml:36

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1482523563.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1674404664.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/992465164.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/668291877.run(Unknown Source)
    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$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/1584193862.run(Unknown Source)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field sample.Controller.login to javafx.scene.control.TextField
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:758)
    at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1155)
    at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:853)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:747)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
    ... 22 more
like image 754
Mich44 Avatar asked Jul 14 '15 18:07

Mich44


1 Answers

Your problem is in import in Controller class:

import java.awt.*;

Should be:

import javafx.scene.control.TextField;

The programs tries to convert java.awt.TextField to javafx.scene.control.TextField

I assume you'd like to use javafx, not AWT TextField

Note: don't trust the imports which IDE gives you :)

like image 121
maskacovnik Avatar answered Nov 15 '22 07:11

maskacovnik