Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Assignment6;
- import java.awt.*;
- import java.text.NumberFormat;
- import java.util.ArrayList;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.event.ActionEvent; //**Need to import
- import javafx.event.EventHandler; //**Need to import
- import javafx.scene.paint.Color;
- import javafx.scene.paint.Paint;
- //import all other necessary javafx classes here
- //----
- public class CreatePane extends HBox
- {
- ArrayList<Club> clubList;
- //The relationship between CreatePane and SelectPane is Aggregation
- private SelectPane selectPane;
- private Label topText, title, members, univ;
- private TextField titleInput, membersInput, univInput;
- private Button create;
- private TextArea area;
- //constructor
- public CreatePane(ArrayList<Club> list, SelectPane sePane)
- {
- this.clubList = list;
- this.selectPane = sePane;
- this.topText = new Label("\n");
- this.title = new Label("Title");
- this.members = new Label("Number Of Members");
- this.univ = new Label("University");
- this.create = new Button("Create a Club");
- this.titleInput = new TextField("");
- this.membersInput = new TextField("");
- this.univInput = new TextField("");
- this.area = new TextArea();
- this.getChildren().addAll(topText,title,members,univ,create,titleInput,membersInput,univInput);
- //initialize each instance variable (textfields, labels, textarea, button, etc.)
- //and set up the layout
- //----
- //constants to gridpane
- GridPane grid = new GridPane();
- grid.setPadding(new Insets(10,10,10,10));
- grid.setVgap(10);
- grid.setHgap(10);
- GridPane.setConstraints(title, 0, 0);
- GridPane.setConstraints(titleInput, 1, 0);
- GridPane.setConstraints(members, 0, 1);
- GridPane.setConstraints(membersInput, 1, 1);
- GridPane.setConstraints(univ, 0, 2);
- GridPane.setConstraints(univInput, 1, 2);
- GridPane.setConstraints(create, 1, 3);
- grid.getChildren().addAll(title,members,univ,create,titleInput,membersInput,univInput);
- area.setVisible(true);
- area.resizeRelocate(0, 20, 450, 380);
- for (Club each : clubList)
- {
- area.appendText(area.getText()+each.toString());
- }
- this.getChildren().addAll(grid,area);
- //You might need to create a sub pane to hold the button
- //----
- //Set up the layout for the left half of the CreatePane.
- //----
- //the right half of the CreatePane is simply a TextArea object
- //Note: a ScrollPane will be added to it automatically when there are no
- //enough space
- //Add the left half and right half to the CreatePane
- //Note: CreatePane extends from HBox
- //----
- ButtonHandler handle = new ButtonHandler();
- create.setOnAction(handle);
- } //end of constructor
- //Create a ButtonHandler class
- //ButtonHandler listens to see if the button "Create" is pushed or not,
- //When the event occurs, it get a club's Title, its number of members, and its university
- //information from the relevant text fields, then create a new club and add it inside
- //the clubList. Meanwhile it will display the club's information inside the text area.
- //using the toString method of the Club class.
- //It also does error checking in case any of the textfields are empty,
- //or a non-numeric value was entered for its number of members
- private class ButtonHandler implements EventHandler<ActionEvent>
- {
- public void handle(ActionEvent event)
- {
- //check to see if any input is empty
- if (titleInput.getText().equalsIgnoreCase("") || membersInput.getText().equalsIgnoreCase("") || univInput.getText().equalsIgnoreCase("") )
- {
- topText.setText("Please enter all fields\n");
- topText.setTextFill(Color.RED);
- return;
- }
- String title = titleInput.getText();
- String univ = univInput.getText();
- int members;
- //check to see if second entry is integer
- try {
- members = Integer.parseInt(membersInput.getText());
- } catch (NumberFormatException e) {
- topText.setText("Please enter an integer for a number of members\n");
- topText.setTextFill(Color.RED);
- return;
- }
- for (Club each : clubList)
- {
- if (each.getClubName().equalsIgnoreCase(title))
- {
- topText.setText("Club not added - duplicate\n");
- topText.setTextFill(Color.RED);
- return;
- }
- }
- //creation of club
- Club newClub = new Club(title,members,univ);
- selectPane.updateClubList(newClub);
- area.appendText(newClub.toString());
- topText.setText("");
- //at the end, don't forget to update the new arrayList
- //information on the SelectPanel
- //----
- //}
- } //end of handle() method
- } //end of ButtonHandler class
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement