I am working on a simple GUI that takes first name, last name, phone, etc with TextField
s. 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:
- Retrieved the specific
TextField
from theVBox
'sNode
. - Cast the
Node
to aTextField
- Made one handler method that determines which button has been pressed.
- 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:
- I've put my
TextField
s in aVBox
, in a method that returns the entireVBox
. - I've made my
Button
s in anHBox
, in a method that returns the entire HBox. - I made my
GridPane
in a method that returns aGridPane
. It is in this method I call the methods that return theVBox
with theTextField
s and theHBox
with theButton
s (and handler method) - The
GridPane
method is called in the start method.