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 Finder extends Application implements EventHandler
//Window attributes private Stage stage; private Scene scene;
//GUI components private TextArea txtArea = new TextArea(); private ScrollPane scrollPane = new ScrollPane(txtArea); private Label lblFind = new Label("Find: "); private TextField findField = new TextField(); private Button btnFind = new Button("Find"); private Button btnClear = new Button("Clear"); // Array of all buttons private Button[] btnAll = { btnFind, btnClear }; //Main program public static void main(String[] args) { launch(args); }
//Constructor public void start(Stage _stage) { stage = _stage; stage.setTitle("Find"); txtArea.setPrefSize(350, 350); txtArea.setWrapText(true); GridPane root = new GridPane(); root.setAlignment(Pos.CENTER); root.setHgap(10); root.setVgap(10); root.setPadding(new Insets(25, 25, 25, 25)); root.add(scrollPane, 0, 0, 2, 1); root.add(lblFind, 0, 1); root.add(findField, 1, 1); root.add(btnFind, 0, 2); root.add(btnClear, 1, 2); //Set up action listeners for(Button button : btnAll) { button.setOnAction(this); } scene = new Scene(root, 350, 400); stage.setScene(scene); stage.show(); } public void handle(ActionEvent ae) { String command = ((Button)ae.getSource()).getText(); switch(command) { case "Find": doFind(); break; case "Clear": findField.setText(""); break; } } //Alert message to be displayed if any to the user private void alert(String title, String message, AlertType alertType) { Alert alert = new Alert(alertType); alert.setTitle(title); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } //find method private void doFind() { String findWord = findField.getText(); int index = txtArea.getText().indexOf(findWord); if (index != -1) { txtArea.selectRange(index, index+findWord.length()); txtArea.requestFocus(); } else { alert("Error", "No Match found", AlertType.ERROR); } }
}
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