0

I'm new to java and I know this is a very basic Question but I keep getting these run time errors, I have created a method and added that method to the event handling section in scene builder but it's not working, please have a look at my code and see what's wrong.(edited)

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import sun.applet.Main;

import java.io.IOException;


public  class registration extends Application {
    // Button button;

    @Override

    public void start(Stage primaryStage)throws Exception{
    primaryStage.setTitle("Event Handling");

    FXMLLoader loader = new 
                  FXMLLoader(registration.class.getResource("jfs.fxml"));
    Parent root = loader.load();
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();

    }
  @FXML
  private Button submit;
  @FXML
  private TextField text;

  @FXML
private void initialize() {
    // Handle Button event.
    submit.setOnAction((event) -> {
        text.appendText("somthn");
    });

    public static void main(String[] args) {
        launch(args);

    }
}

fxml part- I have added the method- intialize to the button onAction but the function does not work when I run it.

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

             <?import javafx.geometry.Insets?>
             <?import javafx.scene.Cursor?>
             <?import javafx.scene.control.Button?>
             <?import javafx.scene.control.CheckBox?>
             <?import javafx.scene.control.Label?>
             <?import javafx.scene.control.PasswordField?>
             <?import javafx.scene.control.TextArea?>
             <?import javafx.scene.control.TextField?>
             <?import javafx.scene.layout.AnchorPane?>
             <?import javafx.scene.layout.VBox?>
             <?import javafx.scene.text.Font?>

             <AnchorPane nodeOrientation="LEFT_TO_RIGHT" prefHeight="530.0" prefWidth="832.0" style="-fx-background-color: pink;"
                         xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="registration">
                 <children>
                     <VBox layoutX="347.0" layoutY="71.0" prefHeight="359.0" prefWidth="433.0" style="-fx-background-color: AliceBlue;">
                         <children>
            <TextField id="name" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="Username"
                       style="-fx-background-color: white; -fx-background-radius: 0px;" VBox.vgrow="SOMETIMES">
               <cursor>
                  <Cursor fx:constant="DEFAULT" />
               </cursor>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
               <VBox.margin>
                  <Insets left="29.0" right="10.0" top="30.0" />
               </VBox.margin>
               <padding>
                  <Insets top="10.0" />
               </padding>
            </TextField>
            <TextField id="e-mail" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="e-mail" VBox.vgrow="SOMETIMES">
               <VBox.margin>
                  <Insets left="29.0" right="10.0" top="30.0" />
               </VBox.margin>
               <padding>
                  <Insets top="10.0" />
               </padding>
            </TextField>
            <PasswordField id="pwd" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="new password">
               <VBox.margin>
                  <Insets left="29.0" right="10.0" top="30.0" />
               </VBox.margin>
            </PasswordField>
            <PasswordField id="pwd" alignment="TOP_CENTER" layoutX="39.0" layoutY="164.0" maxWidth="-Infinity" prefWidth="200.0" promptText="confirm password">
               <VBox.margin>
                  <Insets left="29.0" right="10.0" top="30.0" />
               </VBox.margin>
            </PasswordField>
            <Button mnemonicParsing="false" prefHeight="38.0" prefWidth="75.0" text="Register">
               <VBox.margin>
                  <Insets left="160.0" top="70.0" />
               </VBox.margin>
            </Button>
         </children>
         <padding>
            <Insets right="10.0" top="10.0" />
         </padding></VBox>
      <Label layoutX="353.0" layoutY="34.0" text="Please fill in the following details to register">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
      <VBox id="labelo" layoutX="14.0" layoutY="72.0" prefHeight="359.0" prefWidth="320.0" style="-fx-background-color: AliceBlue;">
         <children>
            <Label text="Welcome to JFS ">
               <font>
                  <Font name="System Bold Italic" size="15.0" />
               </font>
               <VBox.margin>
                  <Insets left="50.0" top="30.0" />
               </VBox.margin>
            </Label>
            <Label />
            <TextField id="mail" alignment="TOP_CENTER" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="200.0" promptText="E-mail">
               <VBox.margin>
                  <Insets left="30.0" top="30.0" />
               </VBox.margin>
            </TextField>
            <Label text="Your query:">
               <VBox.margin>
                  <Insets left="30.0" top="20.0" />
               </VBox.margin>
            </Label>
            <TextArea id="query" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="91.0" prefWidth="213.0">
               <padding>
                  <Insets top="5.0" />
               </padding>
               <VBox.margin>
                  <Insets left="30.0" top="10.0" />
               </VBox.margin>
            </TextArea>
            <CheckBox id="check" mnemonicParsing="false" text="Receive newsletters">
               <VBox.margin>
                  <Insets left="120.0" top="20.0" />
               </VBox.margin>
            </CheckBox>
            <Label id="lab" prefHeight="17.0" prefWidth="104.0">
               <VBox.margin>
                  <Insets left="200.0" right="40.0" top="90.0" />
               </VBox.margin></Label>
            <TextField id="text" fx:id="text" visible="false" />
            <Button id="submit" fx:id="submit" mnemonicParsing="false" onAction="#initialize" text="Submit">

               <VBox.margin>
                  <Insets left="100.0" top="20.0" />
               </VBox.margin>
            </Button>
         </children>
      </VBox>
   </children>
   <cursor>
      <Cursor fx:constant="MOVE" />
   </cursor>
</AnchorPane>
rianne
  • 31
  • 1
  • 5
  • What runtime errors are you getting? Please include the full stack trace. – Itai Jul 22 '18 at 06:50
  • Also, see [Can application class be the controller](https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class) - it sounds like it may be a duplicate, but I can't tell without more details. – Itai Jul 22 '18 at 06:51
  • unrelated: please learn java naming conventions and stick to them – kleopatra Jul 22 '18 at 09:30
  • Also using an `abstract` class as application class will fail to run, since JavaFX won't be able to create a instance of the `Application` class that should be launched... One more suspicious thing is that you're retrieving the resource via some `sun.applet.Main` You need to include the stack trace/description of the error though `sun.applet.Main`. `sun.applet` is probably not the package containing the resource though. – fabian Jul 22 '18 at 10:17
  • Also the fxml may be required depending on the error... – fabian Jul 22 '18 at 10:23
  • @Itai I have fixed my errors and now it runs succesfully but nothing happens when I click the button, I have edited my code, please go through it. – rianne Jul 23 '18 at 03:46
  • @fabian noted and my code runs successfully but nothing happen when I click the button – rianne Jul 23 '18 at 03:47
  • `onAction="#initialize"` This the wrong use of the `initialize` method: this method is supposed to be invoked by `FXMLLoader` after it creates all elements for a fxml. This attribute could simply be removed, since you overwrite the value in the `initialize` method anyways. (you could move the code of the lambda to a seperate method though and use it's name in the attribute). The actual issue though seems to be that you're modifying a node with the attribute `visible="false"` without changing the visibility. The event handler runs but has no effect on the rendered result... – fabian Jul 23 '18 at 08:33
  • @fabian thanks alot :) – rianne Jul 23 '18 at 09:23

0 Answers0