I need to add a close button to my existing JAVA FX code so that when clicked th
ID: 3819717 • Letter: I
Question
I need to add a close button to my existing JAVA FX code so that when clicked the window will close and shut down the program. Below is the existing code.
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<String> items = FXCollections.observableArrayList();
ListView<String> list = new ListView<>(items);
list.setEditable(true);
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
return new TextFieldListCell<>(new StringConverter<String>() {
@Override
public String toString(String object) {
return object;
}
@Override
public String fromString(String string) {
return string;
}
});
}
});
Button btn = new Button();
btn.setText("Add Course");
btn.setOnAction((ActionEvent event) ->
{
String c = "Enter the subject name";
list.getItems().add(list.getItems().size(), c);
list.scrollTo(list.getItems().size() - 1);
new AnimationTimer()
{
int frameCount = 0 ;
@Override
public void handle(long now) {
frameCount++ ;
if (frameCount > 1) {
list.edit(list.getItems().size() - 1);
stop();
}
}
}.start();
});
VBox root = new VBox(list, btn);
Scene scene = new Scene(root);
primaryStage.setTitle("Student Information System");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<String> items = FXCollections.observableArrayList();
ListView<String> list = new ListView<>(items);
list.setEditable(true);
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param) {
return new TextFieldListCell<>(new StringConverter<String>() {
@Override
public String toString(String object) {
return object;
}
@Override
public String fromString(String string) {
return string;
}
});
}
});
Button btn = new Button();
btn.setText("Add Course");
btn.setOnAction((ActionEvent event) ->
{
String c = "Enter the subject name";
list.getItems().add(list.getItems().size(), c);
list.scrollTo(list.getItems().size() - 1);
new AnimationTimer()
{
int frameCount = 0 ;
@Override
public void handle(long now) {
frameCount++ ;
if (frameCount > 1) {
list.edit(list.getItems().size() - 1);
stop();
}
}
}.start();
});
VBox root = new VBox(list, btn);
Scene scene = new Scene(root);
primaryStage.setTitle("Student Information System");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.