WELCOME
PROJECTS
CV
CONTACT
Shelter window project
Previous
Next
Source code
package sheltergx; import animalModel.Animal; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Optional; import javafx.geometry.Insets; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.print.PrinterJob; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javafx.scene.control.cell.CheckBoxListCell; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.layout.GridPane; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Callback; public class ShelterGX extends Application { public static void main(String[] args) { launch(args); } ListView
listView = new ListView<>(); @Override public void start(Stage primaryStage) throws Exception{ int holdPlaces = 0; int freePlaces = 0; int capacity = 0; //selecting path to images FileInputStream addInput = new FileInputStream("icons/buttons/add.jpg"); FileInputStream deleteInput = new FileInputStream("icons/buttons/delete.jpg"); FileInputStream statusInput = new FileInputStream("icons/buttons/status.jpg"); FileInputStream quitInput = new FileInputStream("icons/buttons/exit.jpg"); //creating images Image addImage = new Image(addInput); Image deleteImage = new Image(deleteInput); Image statusImage = new Image(statusInput); Image quitImage = new Image(quitInput); //creating imageView which will be used as background in buttons ImageView addView = new ImageView(addImage); ImageView deleteView = new ImageView(deleteImage); ImageView statusView = new ImageView(statusImage); ImageView quitView = new ImageView(quitImage); //creating window buttons and adding text and images to background Button add = new Button("", addView); Button delete = new Button("", deleteView); Button status = new Button("", statusView); Button quit = new Button("", quitView); add.setStyle("-fx-min-width: 200px; -fx-max-width: 200px; -fx-min-height: 200px; -fx-max-height: 200px; -fx-margin: 10px;"); delete.setStyle("-fx-min-width: 200px; -fx-max-width: 200px; -fx-min-height: 200px; -fx-max-height: 200px; -fx-margin: 10px;"); status.setStyle("-fx-min-width: 200px; -fx-max-width: 200px; -fx-min-height: 200px; -fx-max-height: 200px; -fx-margin: 10px;"); quit.setStyle("-fx-min-width: 200px; -fx-max-width: 200px; -fx-min-height: 200px; -fx-max-height: 200px; -fx-margin: 10px;"); //creating labels for buttons Label addLabel = new Label("Add an animal"); Label deleteLabel = new Label("Delete an animal"); Label statusLabel = new Label("Status"); Label quitLabel = new Label("Quit"); //setting action which will be performed if 'add' button is clicked add.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //creating labels for input fieds and setting their text Label nameLab = new Label("Name: "); Label ageLab = new Label("Age: "); //creating input fields for name and age TextField nameField = new TextField(); TextField ageField = new TextField(); //creating buttons for child window and adding text on them Button addAnimal = new Button("Add"); Button quitAdding = new Button("Quit"); /* ==============ACTIONS FOR CHILD BUTTONS============== */ //adding action for 'add' button addAnimal.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //creating variables for name and age String name = ""; String age = ""; //assigning inputs for name and age to variables name = nameField.getText(); age = ageField.getText(); /* runs continuously while(name.equals("")){ Alert alert = new Alert(AlertType.ERROR); alert.setTitle("ERROR"); alert.setHeaderText(null); alert.setContentText("Ooops, name can\'t be empty. Please fill it up."); alert.showAndWait(); //name = nameField.getText(); } while(age.equals("")){ Alert alert = new Alert(AlertType.ERROR); alert.setTitle("ERROR"); alert.setHeaderText(null); alert.setContentText("Ooops, age can\'t be empty. Please fill it up."); alert.showAndWait(); //age = ageField.getText(); } */ //creating instance of 'Animal', setting its name, age, and checked value. Checked value is helpful for //checkBoxes displayed in list Animal animal = new Animal(name, age, false); //adding listener for checked value. Information is given about its input. animal.checkProperty().addListener((obs, wasOn, isNowOn) -> { if(isNowOn == true){ System.out.println(animal.getName() + " is checked to delete"); } else{ System.out.println(animal.getName() + " is unchecked"); } }); //adding an 'Animal' object to list of all animals. List will be used to displaying animals. listView.getItems().add(animal); //resetting values for name and age nameField.setText(""); ageField.setText(""); } }); //adding action for 'quit' button quitAdding.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ Stage stage = (Stage) quitAdding.getScene().getWindow(); stage.close(); } }); /* ==============END OF CHILD BUTTONS============== */ //creating main panel for child window GridPane childPane = new GridPane(); childPane.setMinSize(300, 300); childPane.setAlignment(Pos.CENTER); //setting components on main panel in 3x2 format(rows X columns) childPane.add(nameLab, 0, 0); childPane.add(nameField, 1, 0); childPane.add(ageLab, 0, 1); childPane.add(ageField, 1, 1); childPane.add(addAnimal, 0, 2); childPane.add(quitAdding, 1, 2); //creating scene for child form and setting scene size Scene childScene = new Scene(childPane, 400, 400); childScene.setOnKeyPressed(new EventHandler
(){ @Override public void handle(KeyEvent event){ switch(event.getCode()){ case ESCAPE: quitAdding.fire(); break; case ENTER: addAnimal.fire(); nameField.requestFocus(); break; } } }); //creating child stage Stage childStage = new Stage(); //adding title to child window childStage.setTitle("Child window"); //adding scene to child window childStage.setScene(childScene); //making child window visible childStage.show(); } }); //setting action for 'delete' button delete.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //Defining how rows of animals list should be displayed listView.setCellFactory(CheckBoxListCell.forListView(new Callback
>(){ @Override //adding checkboxes for animals public ObservableValue
call(Animal animal){ return animal.checkProperty(); } //adding name and age to be displayed in row for each animal public ListCell
call(ListView
a){ ListCell
cell = new ListCell
(){ @Override protected void updateItem(Animal t, boolean bln){ super.updateItem(t, bln); if(t != null){ setText(t.getName() + ", " + t.getAge()); } } }; return cell; } })); //creating panel for child window. Holds two panels created below. VBox root = new VBox(); //creating panels for list and buttons StackPane listPane = new StackPane(); GridPane buttonPane = new GridPane(); //creating buttons Button deleteButton = new Button("Delete animal"); Button deleteQuit = new Button("Quit"); //adding action for 'delete' button. Deletes selected animal(s) deleteButton.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //creating list of animals which should be deleted ArrayList
listToDelete = new ArrayList<>(); //iterating through list of animals for(int i = 0; i < listView.getItems().size(); i++){ //if animal has checked checkbox it name is displayed and animal is added to list to be deleted if(listView.getItems().get(i).isCheck() == true){ listToDelete.add(listView.getItems().get(i)); } } //if there is animal(s) to delete confirmation window pops up. if(listToDelete.size() > 0){ //creating confirmation window and setting its type; adding title; adding background text; Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("Deleting confirmation"); alert.setHeaderText("Are you sure you want to delete those animals?"); //adding variable whichc holds type of button clicked Optional
result = alert.showAndWait(); //if button clicked is 'OK' button, list of animals which should be deleted is deleted from list of all animals //and information about it is displayed if(result.get() == ButtonType.OK){ listView.getItems().removeAll(listToDelete); System.out.println("Animals successfully deleted."); } //if cancel button is clicked it closes confirmation window else{ alert.close(); } } } }); //adding action for 'quit' button deleteQuit.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //gets window object and closes it Stage stage = (Stage) deleteQuit.getScene().getWindow(); stage.close(); } }); //adding buttons to their panel and seting them next to each other buttonPane.add(deleteButton, 0, 0); buttonPane.add(deleteQuit, 1, 0); //adding list to its panel listPane.getChildren().add(listView); //adding button panel and list panel to main panel root.getChildren().add(listPane); root.getChildren().add(buttonPane); //creating main scene and setting its size Scene deleteScene = new Scene(root, 400, 400); //adding action for ENTER key deleteScene.setOnKeyPressed(new EventHandler
(){ @Override public void handle(KeyEvent event){ switch(event.getCode()){ case ENTER: if(listView.getItems().size() > 0){ deleteButton.fire(); } break; case ESCAPE: deleteQuit.fire(); break; } } }); //creating stage for child form;setting its title;setting scene for it;making window visible Stage childStage = new Stage(); childStage.setTitle("delete animals"); childStage.setScene(deleteScene); childStage.show(); //adding focus for deleteButton. If I put it anywhere else, focus would be allways on listView variable. //As keyevents can be fired on root or root children and button is a level lower I have to give a focus for proper node. deleteButton.requestFocus(); } }); //adding action for 'status' button status.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ //creating variables for calculating free and held places int capacity = 20; int holdPlaces = listView.getItems().size(); int freePlaces = capacity - holdPlaces; //creating new pie chart PieChart pieChart = new PieChart(); //adding data for pie chart slices and setting their titles PieChart.Data slice_free = new PieChart.Data("Free places", freePlaces); PieChart.Data slice_hold = new PieChart.Data("Held places", holdPlaces); //adding slices to pie chart pieChart.getData().add(slice_free); pieChart.getData().add(slice_hold); //creating button for printing Button printButton = new Button("Print"); //creating main pane VBox vbox = new VBox(); //adding button and pie chart to pane vbox.getChildren().add(printButton); vbox.getChildren().add(pieChart); //creating child scene Scene statusScene = new Scene(vbox, 400, 200); //adding actions to ESCAPE button statusScene.setOnKeyPressed(new EventHandler
(){ @Override public void handle(KeyEvent event){ switch(event.getCode()){ //ENTER launches print function case ENTER: printButton.fire(); break; //ESCAPE closes window case ESCAPE: Stage stage = (Stage) statusScene.getWindow(); stage.close(); } } }); //creating new stage Stage pieStage = new Stage(); //adding title, scene, setting size and showing window pieStage.setTitle("Pie chart"); pieStage.setScene(statusScene); pieStage.setHeight(400); pieStage.setWidth(400); pieStage.show(); //adding action to print button, which sets up printer printButton.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ printSetup(listView, pieStage); } }); } }); //ading action for 'quit' button. It closes whole application quit.setOnAction(new EventHandler
(){ @Override public void handle(ActionEvent event){ Platform.exit(); } }); //creating main panel for holding buttons panels GridPane root = new GridPane(); //creating panes for button and label VBox addPane = new VBox(); VBox deletePane = new VBox(); VBox statusPane = new VBox(); VBox quitPane = new VBox(); //adding buttons and labels to panes;setting spaces between buttons; aligninng components to center of button addPane.getChildren().addAll(add, addLabel); addPane.setSpacing(5); addPane.setAlignment(Pos.CENTER); deletePane.getChildren().addAll(delete, deleteLabel); deletePane.setSpacing(5); deletePane.setAlignment(Pos.CENTER); statusPane.getChildren().addAll(status, statusLabel); statusPane.setSpacing(5); statusPane.setAlignment(Pos.CENTER); quitPane.getChildren().addAll(quit, quitLabel); quitPane.setSpacing(5); quitPane.setAlignment(Pos.CENTER); //setting gaps between buttons root.setVgap(15); root.setHgap(15); //setting size, padding and alignment on panel root.setMinSize(150, 100); root.setPadding(new Insets(10, 10, 10, 10)); root.setAlignment(Pos.TOP_LEFT); //adding buttons to panel( row x colum) root.add(addPane, 0, 0); root.add(deletePane, 1, 0); root.add(statusPane, 0, 1); root.add(quitPane, 1, 1); //creating new scene for panel Scene scene = new Scene(root); //adding functionality to enter button which fires action of focused button scene.setOnKeyPressed(new EventHandler
(){ @Override public void handle(KeyEvent event){ switch(event.getCode()){ case ENTER: if(scene.focusOwnerProperty().get() instanceof Button){ Button focusedButton = (Button) scene.focusOwnerProperty().get(); focusedButton.fire(); } break; } } }); //adding title for window primaryStage.setTitle("Shelter App 1.0"); //adding scene to window primaryStage.setScene(scene); //making window visible primaryStage.show(); } //functions used for setting up printer private void printSetup(Node node, Stage owner){ PrinterJob job = PrinterJob.createPrinterJob(); if(job == null){ return; } boolean proceed = job.showPrintDialog(owner); if(proceed){ print(job, node); } } //function used to printing private void print(PrinterJob job, Node node){ boolean printed = job.printPage(node); if(printed){ job.endJob(); System.out.println("Document has been printed."); } } }
Download window code
Animal model
package animalModel; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; public class Animal { private String name; private String age; private BooleanProperty check = new SimpleBooleanProperty(); public Animal(){} public Animal(String name, String age){ this.name = name; this.age = age; } public Animal(String name, String age, boolean check){ this.name = name; this.age = age; setCheck(check); } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getAge(){ return age; } public void setAge(String age){ this.age = age; } public BooleanProperty checkProperty(){ return this.check; } public boolean isCheck(){ return this.checkProperty().get(); } public void setCheck(boolean check){ this.checkProperty().set(check); } @Override public String toString(){ return "" + getName() + ", " + getAge(); } }
Download animal model code