Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Assignment #: Arizona State University CSE205 #6
- //Name: Summer Beckman
- //StudentID: 1216922252
- //Lecture: MW 4:35
- //Description: CreatePane generates a pane where a user can enter
- //a club information and create a list of available clubs.
- //import all other necessary javafx classes here
- //----
- import java.util.ArrayList;
- import javafx.scene.layout.HBox;
- import javafx.scene.paint.Color;
- import javafx.event.ActionEvent; //**Need to import
- import javafx.event.EventHandler; //**Need to import
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.layout.*;
- import javafx.scene.control.*;
- import javafx.scene.control.Button;
- public class CreatePane extends HBox
- {
- ArrayList<Club> clubList;
- //The relationship between CreatePane and SelectPane is Aggregation
- private SelectPane SelectPane;
- private TextArea textSpace;
- private Button create;
- private TextField text1, text2, text3;
- private Label titleLabel, memberLabel, uniLabel, infoText;
- //constructor
- public CreatePane(ArrayList<Club> list, SelectPane sePane)
- {
- this.clubList = list;
- this.SelectPane = sePane;
- //initialize each instance variable (textfields, labels, textarea, button, etc.)
- //and set up the layout
- //----
- this.titleLabel = new Label("Title");
- this.memberLabel = new Label("Number of Members");
- this.uniLabel = new Label("University");
- this.create = new Button("Create a club");
- this.infoText = new Label("/n");
- this.textSpace = new TextArea();
- this.text1 = new TextField("");
- this.text2 = new TextField("");
- this.text3 = new TextField("");
- this.getChildren().addAll(titleLabel, memberLabel,uniLabel, create, textSpace, text1, text2, text3);
- //create a GridPane hold those labels & text fields.
- //you can choose to use .setPadding() or setHgap(), setVgap()
- //to control the spacing and gap, etc.
- //----
- GridPane centerPane = new GridPane();
- centerPane.setHgap(10);
- centerPane.setVgap(10);
- centerPane.setPadding(new Insets(10, 10, 10, 10));
- centerPane.setAlignment(Pos.CENTER);
- //You might need to create a sub pane to hold the button
- //----
- GridPane.setConstraints(uniLabel, 0, 2);
- GridPane.setConstraints(text3, 1, 2);
- GridPane.setConstraints(memberLabel, 0, 1);
- GridPane.setConstraints(text2, 1, 2);
- GridPane.setConstraints(titleLabel, 0, 0);
- GridPane.setConstraints(text1, 1, 2);
- GridPane.setConstraints(create, 1, 3);
- centerPane.getChildren().addAll(uniLabel, text3, memberLabel, text2, titleLabel, text1);
- //register/link source object with event handler
- //----
- textSpace.setVisible(true);
- textSpace.resizeRelocate(0, 40, 500, 400);
- for (Club each : clubList)
- {
- textSpace.appendText(textSpace.getText() + each.toString());
- }
- } //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>
- {
- //Override the abstact method handle()
- public void handle(ActionEvent event)
- {
- //declare any necessary local variables here
- //---
- int numMembers;
- String title = text1.getText();
- String members = text3.getText();
- String university = text2.getText();
- //when a text field is empty and the button is pushed
- //handle the case here
- if (title.equalsIgnoreCase("") || university.equalsIgnoreCase("") || members.equalsIgnoreCase(""))
- {
- infoText.setTextFill(Color.RED);
- infoText.setText("Please fill all fields.");
- return;
- } else {
- //when a non-numeric value was entered for its number of members
- //and the button is pushed
- //you will need to use try & catch block to catch
- //the NumberFormatException
- //----
- try {
- numMembers = Integer.parseInt(members);
- }
- catch
- (NumberFormatException e){
- infoText.setTextFill(Color.RED);
- infoText.setText("Please enter a integer for a number of members.");
- return;
- }
- //When a club of an existing club name in the list
- //was attempted to be added, do not add it to the list
- //and display a message "Club not added - duplicate"
- for (Club each : clubList)
- {
- if (each.getClubName().equalsIgnoreCase(title));
- {
- infoText.setTextFill(Color.RED);
- infoText.setText("Club not added - duplicate");
- return;
- }
- }
- //at the end, don't forget to update the new arrayList
- //information on the SelectPanel
- //----
- infoText.setText("");
- Club club = new Club(titleLabel.getText(), numMembers, uniLabel.getText());
- SelectPane.updateClubList(club);
- textSpace.appendText(club.toString());
- }
- } //end of handle() method
- } //end of ButtonHandler class
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement