-1

I am working on a simple GUI that takes first name, last name, phone, etc with TextFields. I have buttons like "New", "Update", and "Delete" so you can do stuff to the entered data.

I'm still new to JavaFX, so I'm not 100% confident in my GUI design approach, and fear that it may have something to do with me not being able to retrieve the entered values from a given TextField. I am sure getText() is working, because if I set the text in the TextField, then use getText(), I can retrieve the data in the TextField after pressing the button. What seems to be happening is the handler method is returning the TextField's default value. I think this because there is an empty line above the confirmation "New Contact Created."

What I've done:

  1. Retrieved the specific TextField from the VBox's Node.
  2. Cast the Node to a TextField
  3. Made one handler method that determines which button has been pressed.
  4. Try to get the entered text from the TextField
Node firstName = addTextFieldVBox().getChildren().get(0);
TextField first = (TextField)firstName;
Button newContact = new Button ("New");

EventHandler handle = new EventHandler()
{   
    @Override
    public void handle(Event event)
    {
        //determine which button is being pressed
        //get the button's text for switch case
        Button button = ((Button)event.getSource());
        String currentButton = button.getText();

        switch(currentButton)
        {
            case "New":
                System.out.println(first.getText());
                System.out.println("New Contact Created.");
                break;
             default:
                System.out.println("Error with buttons");
        }  
    }
};

newContact.setOnAction(handle);

If it helps to know more about my design:

  1. I've put my TextFields in a VBox, in a method that returns the entire VBox.
  2. I've made my Buttons in an HBox, in a method that returns the entire HBox.
  3. I made my GridPane in a method that returns a GridPane. It is in this method I call the methods that return the VBox with the TextFields and the HBox with the Buttons (and handler method)
  4. The GridPane method is called in the start method.
fabian
  • 80,457
  • 12
  • 86
  • 114
Bobemius
  • 21
  • 4
  • 1
    Your explanation is bringing us nowhere. Instead of posting partial codes that we don't understand what you are trying to do, please post a [MCVE]. – Jai Mar 21 '18 at 04:39
  • Are you sure `addTextFieldVBox()` doesn't create a new `VBox` instead of accessing the existing one? – fabian Mar 21 '18 at 08:49

1 Answers1

0

As your question is very broad and difficult to understand I going to give you another idea of how to do this with a Hashmap and it will be easy to pass around as well as knowing exactly which textfield your working with other than addTextFieldVBox().getChildren().get(0); . Caution this will require more coding but will be easier for someone else to follow if they look at your code.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.HashMap;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        HashMap<String, TextField>  textFieldHashMap = new HashMap<>();

        TextField firstNameTextField = new TextField("Johnnnnnnny");
        textFieldHashMap.put("firstName", firstNameTextField);

        Button getFirstName = new Button("Get it");
        getFirstName.setOnAction(event -> System.out.println(textFieldHashMap.get("firstName").getText()));

        VBox vBox = new VBox();
        vBox.getChildren().addAll(firstNameTextField, getFirstName);

        Scene scene = new Scene(vBox);
        stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}
Matt
  • 3,052
  • 1
  • 17
  • 30