0

I am new to programming. My problem right now is that I want to ask the user to give his name in a TextField, and use that value(name) in another BoardGame class. for example, first window to ask his name, and second window to use it in the game.

My View class (first window)

public class View extends BorderPane {

public TextArea player1;
public Label name1;

public View(){
    super();
    initializeNodes();
    layoutNodes();
}

public void initializeNodes() {
    player1 = new TextArea();
    name1 = new Label ("Player 1: ");

}
public void layoutNodes() {
    player1.setPrefSize(100,20);
    naam1.setPrefSize(60,40);

    HBox hBox = new HBox(name1,player1);
    TextField nameField;
    nameField = new TextField();
    nameField.setPrefHeight(70);
    player1.getText();
}
EventHandler<ActionEvent> e = new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
        player1.setText(name1.getText());
    }
};
public TextArea getPlayer1() {
    return player1;
}

My BoardGame class(second window) :

public class Boardgame extends BorderPane{
private Label lblPlayer1;
private View view;


public Boardgame(){
    super();
    initializeNodes();
    layoutNodes();
}

private void initializeNodes() {
    super.setPrefSize(900,700);
    lblPlayer1 = new Label("Player 1");
    

}

private void layoutNodes() {
    VBox vBox = new VBox(lblPlayer1);
}

Only thing that works is where I get "Player 1" instead of the name, because I put it hardcoded in there, but how can I make it their input(name)?

aa kk
  • 1
  • 1
    It is hard to give an advice without having the context. How are `Boardgame` and `View` initialized ? One approach is to use a getter, as you did. Another and more robust approach would be to share a `Model` class between `Boardgame` and `View`. For more help post [mre] – c0der Mar 09 '21 at 04:40
  • https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 Mar 09 '21 at 15:22
  • Does this answer your question? [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – Zephyr Mar 10 '21 at 15:27

1 Answers1

-1

I recommend you use the SceneBuilder, it will show you all available FX Components, and make it easier for you to design the UI.

It needs some research since you will need a controller etc (Example here: http://tutorials.jenkov.com/javafx/fxml.html)

However, if you want a way of your user being able to input, Java FX offers the Alert Dialog.

You can see a good example of its usage here: https://code.makery.ch/blog/javafx-dialogs-official/

Dahlin
  • 157
  • 1
  • 11