Question: How to change my code so that it uses VBox instead of the GridPane? import javafx.application.Application; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.control.Alert.*; import

How to change my code so that it uses VBox instead of the GridPane?

import javafx.application.Application; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.control.Alert.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.geometry.*;

public class TextFinder extends Application { TextArea textArea; TextField findField;

@Override public void start(Stage stage) { stage.setTitle("Find"); GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); //Text area textArea = new TextArea(""); //textArea.setText(""); textArea.setPrefSize(350, 350); textArea.setWrapText(true); ScrollPane scrollPane = new ScrollPane(textArea); grid.add(scrollPane, 0, 0, 2, 1); //Find label Label findLabel = new Label("Find: "); grid.add(findLabel, 0, 1); //Find text field findField = new TextField(); grid.add(findField, 1, 1); //Find button Button findButton = new Button("Find"); grid.add(findButton, 0, 2); //Clear button Button clearButton = new Button("Clear"); grid.add(clearButton, 1, 2); findButton.setOnAction(new FindAction()); //Clear button action clearButton.setOnAction( actionEvent -> { findField.setText(""); }); Scene scene = new Scene(grid, 350, 400); stage.setScene(scene); stage.show(); //Closing window action stage.setOnCloseRequest( new EventHandler(){ public void handle(WindowEvent e){ Alert alert = new Alert(AlertType.INFORMATION, "Thank you for using finder"); alert.setHeaderText("Thank you!"); alert.showAndWait(); System.exit(1); } } ); }

//Alert message to be displayed if any to the user public void alert(String title, String message, AlertType alertType) { Alert alert = new Alert(alertType); alert.setTitle(title); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); }

//Execution starts from here public static void main(String[] args) { launch(args); } //Named inner class which handles Find event private class FindAction implements EventHandler { @Override public void handle(ActionEvent event) { // TODO Auto-generated method stub String findWord = findField.getText(); int index = textArea.getText().indexOf(findWord); if (index != -1) { textArea.selectRange(index, index+findWord.length()); textArea.requestFocus(); } else { alert("Error", "No Match found", AlertType.ERROR); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!