The whole goal of this program is to take input from the user : prefix, first name, middle name, and last name and when the button is clicked, combine all of the values. EXAMPLE Mr. John Jack Smith. The issue is when I run the program I end up getting hexadecimal values. How can I get string values instead?
import java.util.*;
import java.io.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.scene.layout.VBox;
import javax.swing.text.JTextComponent;
public class NameFormater extends Application
{
Label lFirstName = new Label("please type first name");
TextField tFirstName = new TextField();
Label lMiddleName = new Label("please type middle name");
TextField tMiddleName = new TextField();
Label lLastName = new Label("please type last name");
TextField tLastName = new TextField();
Label lPrefix = new Label("please type your prefix");
TextField tPrefix = new TextField();
VBox vB;
Scene finalScene;
Stage actualStage = new Stage();
Label labelAll = new Label("maybe this will change");
Scene s;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
Button b = new Button("format");
b.setOnAction(new ButtonClicker());
vB = new VBox(lPrefix, tPrefix, lFirstName, tFirstName, lMiddleName, tMiddleName, lLastName, tLastName, labelAll, b);
s = new Scene(vB);
actualStage.setScene(s);
actualStage.show();
}
class ButtonClicker implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
String fN = tFirstName.toString();
String mN = tMiddleName.toString();
String lN = tLastName.toString();
String tP = tPrefix.toString();
String all = (tPrefix + " " + notHex + tMiddleName + " " + tLastName + " ");
//System.out.println(all);
labelAll.setText(all);
}
}
}
I have tried using .toString() as well as .getText() on the textfields. Sadly both of them give hexadecimal values.
It is not the hexadecimal values. It is what the TextField
s toString()
method is returning. You cannot directly use the textField instance for getting the text. Call getText()
method of the TextField
to get the String value.
By the way,your code looks very verbose or not structured (not sure if it is intended). You can improvise the code as below:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class NameFormater extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Label lFirstName = new Label("please type first name");
TextField tFirstName = new TextField();
Label lMiddleName = new Label("please type middle name");
TextField tMiddleName = new TextField();
Label lLastName = new Label("please type last name");
TextField tLastName = new TextField();
Label lPrefix = new Label("please type your prefix");
TextField tPrefix = new TextField();
Label labelAll = new Label();
Button button = new Button("format");
button.setOnAction(e->{
String fullName = "%s. %s %s %s".formatted(tPrefix.getText(), tFirstName.getText(), tMiddleName.getText(), tLastName.getText());
labelAll.setText(fullName);
});
VBox root = new VBox(10,lPrefix, tPrefix, lFirstName, tFirstName, lMiddleName, tMiddleName, lLastName, tLastName, labelAll, button);
root.setPadding(new Insets(10));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With