Question
What changes that I should do to make the code output the last name instead of the first. JAVA The Main Class: import javafx.application.Application; import
What changes that I should do to make the code output the last name instead of the first.
JAVA
The Main Class:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application{
public void start(Stage primaryStage) {
Scene scene=new Scene(new Practice19(), 300, 150);
primaryStage.setTitle("Find your first name");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[]args) {
launch(args);
}
}
The Operation Class:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.text.*;
public class Practice19 extends GridPane {
private Label result;
private TextField name;
private int count=0;
private Text countText;
public Practice19() {
Font font=new Font(20);
Label inputLabel=new Label("FULL NAME");
inputLabel.setFont(font);
GridPane.setHalignment(inputLabel, HPos.RIGHT);
Label outputLabel=new Label("Last NAME");
outputLabel.setFont(font);
GridPane.setHalignment(outputLabel,HPos.RIGHT);
result=new Label(" ");
result.setFont(font);
GridPane.setHalignment(result, HPos.CENTER);
count=0;
countText=new Text("Pushed: 0");
Button push=new Button("Push Me!");
push.setOnAction(new ButtonHandler());
name=new TextField();
name.setFont(font);
name.setPrefWidth(200);
name.setAlignment(Pos.CENTER);
name.setOnAction(this::processReturn1);
setAlignment(Pos.CENTER);
setHgap(20);
setVgap(20);
setStyle("-fx-background-color:skyblue");
add(inputLabel, 0, 0);
add(name, 1, 0);
add(outputLabel, 0,1);
add(result, 1,1);
add(push,2,0);
add(countText,2,1);
}
public void processReturn1(ActionEvent event) {
String fullName=name.getText();
String firstName=null;
int counter=0;
while(fullName.charAt(counter)!=' ') {
firstName=fullName.substring(0, counter+1);
counter++;
}
result.setText(firstName);
}
private class ButtonHandler implements EventHandler
public void handle(ActionEvent event) {
count++;
countText.setText("Pushes: "+count);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started