0

I'm trying to create a TextField to get a users name. I've created a new class for containing UI components.

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class start extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Name:");
        TextField textField = new TextField();
        HBox hBox = new HBox();
        hBox.getChildren().addAll(label,textField);
        hBox.setSpacing(10);
        Scene scene = new Scene(hBox,300,200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Hello");
        primaryStage.show();
    }
}

I want to get the name entered in the "Hello" window to use it in another class ShowOff.

ShowOff Class

import javafx.application.Application;

public class ShowOff {
    ShowOff() {
        Application.launch(start.class,"myClass");
    }

    public static void main(String[] args)
    {

    }
}

How could I implement that?

Jonah
  • 1,013
  • 15
  • 25
IAmBlake
  • 457
  • 6
  • 21

4 Answers4

1

Your overall structure doesn't really fit into the JavaFX Application lifecycle. A JavaFX application is started by invoking Application.launch(...), which then creates an instance of the application class for you, and calls its start() method. No reference to the instance that is created is returned, and the launch(...) method does not return until the application exits (so there would be little use for it if it did).

So you should really consider the start() method as the entry point to the application, and let that method do nothing more than the startup.

So to get the value from the screen with the "hello" message in the startup class, I would do something like:

public class ShowOff extends Application {

    @Override
    public void start(Stage primaryStage) {
        HelloScreen hello = new HelloScreen();
        primaryStage.setScene(new Scene(hello.getView()));

        // show stage and wait until it closes:
        primaryStage.showAndWait();

        String message = hello.getMessage();
        // do something with message...
        System.out.println(message);
    }

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

and

public class HelloScreen {
    private TextField textField ;
    private VBox view ;

    public HelloScreen() {
        Label label = new Label("Name:");
        textField = new TextField();
        HBox hBox = new HBox(10, label,textField);
        hBox.setAlignment(Pos.CENTER);
        Button button = new Button("Show User Input In Console");

        // On click, close the window:
        button.setOnAction( e -> view.getScene().getWindow().hide());

        view = new VBox(10, hBox, button);
        view.setAlignment(Pos.CENTER);
    }

    public String getMessage() {
        return textField.getText();
    }

    public Parent getView() {
        return view ;
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
0

You would pass a reference of your label into the class. I recommend reading this for more information.

But in regards to your code. Here is how I would revise it:

  public class Start extends Application {

  public static void main(String[] args)  {
      Application.launch(Start.class,"myClass");
  }

  @Override
  public void start(Stage primaryStage) throws Exception {
    Label label = new Label("Name:");
    TextField textField = new TextField();
    HBox hBox = new HBox();
    hBox.getChildren().addAll(label,textField);
    hBox.setSpacing(10);
    Scene scene = new Scene(hBox,300,200);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Hello");
    primaryStage.show();

    textField.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
          //Handle the change in the text value here
        }
    });
  }
}

Now in this case I removed your ShowOff class but if you need some clarification let me know.

0

TextField in the window captures the user input. You can get the text from the TextField and display it anywhere you want. In order to get the current text from the TextField, you need to use getText().

In order to learn and understand how controls and layouts work, I highly recommend going through Getting Started with JavaFX Sample Applications.

Here is a small example which uses a TextField to accept user input and prints the text to the console on the press of the button:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PassParameter extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Name:");
        TextField textField = new TextField();
        HBox hBox = new HBox(10, label,textField);
        hBox.setAlignment(Pos.CENTER);
        Button button = new Button("Show User Input In Console");
        // On click, print the text from the TextField in the console
        button.setOnAction( e -> System.out.println(textField.getText()));
        VBox vBox = new VBox(10, hBox, button);
        vBox.setAlignment(Pos.CENTER);
        Scene scene = new Scene(vBox,300,200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Hello");
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch();
    }
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
-1

To pass off the values from one class to another you need to declare your variable in the first class then in the other class extend the first class

    class PassingVariables
{
    static int hello;
    public static void main( String[] args )
    {
        ShowOff pass = new ShowOff();

    }
}
class ShowOff extends PassingVariables
{
    ShowOff()
    {
        hello = 15;
        System.out.println("Hello = "+hello);

    }
}
Austin
  • 726
  • 4
  • 22