Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. package Assignment6;
  2.  
  3. import java.awt.*;
  4. import java.text.NumberFormat;
  5. import java.util.ArrayList;
  6.  
  7. import javafx.geometry.Insets;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.control.TextArea;
  12. import javafx.scene.control.TextField;
  13. import javafx.scene.layout.GridPane;
  14. import javafx.scene.layout.HBox;
  15.  
  16. import javafx.event.ActionEvent; //**Need to import
  17. import javafx.event.EventHandler; //**Need to import
  18. import javafx.scene.paint.Color;
  19. import javafx.scene.paint.Paint;
  20.  
  21. //import all other necessary javafx classes here
  22. //----
  23.  
  24. public class CreatePane extends HBox
  25. {
  26. ArrayList<Club> clubList;
  27. //The relationship between CreatePane and SelectPane is Aggregation
  28. private SelectPane selectPane;
  29. private Label topText, title, members, univ;
  30. private TextField titleInput, membersInput, univInput;
  31. private Button create;
  32. private TextArea area;
  33.  
  34.  
  35. //constructor
  36. public CreatePane(ArrayList<Club> list, SelectPane sePane)
  37. {
  38. this.clubList = list;
  39. this.selectPane = sePane;
  40.  
  41. this.topText = new Label("\n");
  42. this.title = new Label("Title");
  43. this.members = new Label("Number Of Members");
  44. this.univ = new Label("University");
  45. this.create = new Button("Create a Club");
  46.  
  47. this.titleInput = new TextField("");
  48. this.membersInput = new TextField("");
  49. this.univInput = new TextField("");
  50. this.area = new TextArea();
  51.  
  52. this.getChildren().addAll(topText,title,members,univ,create,titleInput,membersInput,univInput);
  53. //initialize each instance variable (textfields, labels, textarea, button, etc.)
  54. //and set up the layout
  55. //----
  56.  
  57.  
  58. //constants to gridpane
  59. GridPane grid = new GridPane();
  60. grid.setPadding(new Insets(10,10,10,10));
  61. grid.setVgap(10);
  62. grid.setHgap(10);
  63.  
  64. GridPane.setConstraints(title, 0, 0);
  65. GridPane.setConstraints(titleInput, 1, 0);
  66.  
  67. GridPane.setConstraints(members, 0, 1);
  68. GridPane.setConstraints(membersInput, 1, 1);
  69.  
  70. GridPane.setConstraints(univ, 0, 2);
  71. GridPane.setConstraints(univInput, 1, 2);
  72.  
  73. GridPane.setConstraints(create, 1, 3);
  74.  
  75. grid.getChildren().addAll(title,members,univ,create,titleInput,membersInput,univInput);
  76.  
  77. area.setVisible(true);
  78. area.resizeRelocate(0, 20, 450, 380);
  79. for (Club each : clubList)
  80. {
  81. area.appendText(area.getText()+each.toString());
  82. }
  83. this.getChildren().addAll(grid,area);
  84. //You might need to create a sub pane to hold the button
  85. //----
  86.  
  87. //Set up the layout for the left half of the CreatePane.
  88. //----
  89.  
  90.  
  91. //the right half of the CreatePane is simply a TextArea object
  92. //Note: a ScrollPane will be added to it automatically when there are no
  93. //enough space
  94.  
  95. //Add the left half and right half to the CreatePane
  96. //Note: CreatePane extends from HBox
  97. //----
  98. ButtonHandler handle = new ButtonHandler();
  99. create.setOnAction(handle);
  100.  
  101. } //end of constructor
  102.  
  103. //Create a ButtonHandler class
  104. //ButtonHandler listens to see if the button "Create" is pushed or not,
  105. //When the event occurs, it get a club's Title, its number of members, and its university
  106. //information from the relevant text fields, then create a new club and add it inside
  107. //the clubList. Meanwhile it will display the club's information inside the text area.
  108. //using the toString method of the Club class.
  109. //It also does error checking in case any of the textfields are empty,
  110. //or a non-numeric value was entered for its number of members
  111.  
  112. private class ButtonHandler implements EventHandler<ActionEvent>
  113. {
  114. public void handle(ActionEvent event)
  115. {
  116. //check to see if any input is empty
  117. if (titleInput.getText().equalsIgnoreCase("") || membersInput.getText().equalsIgnoreCase("") || univInput.getText().equalsIgnoreCase("") )
  118. {
  119. topText.setText("Please enter all fields\n");
  120. topText.setTextFill(Color.RED);
  121. return;
  122. }
  123.  
  124. String title = titleInput.getText();
  125. String univ = univInput.getText();
  126. int members;
  127.  
  128. //check to see if second entry is integer
  129. try {
  130. members = Integer.parseInt(membersInput.getText());
  131. } catch (NumberFormatException e) {
  132. topText.setText("Please enter an integer for a number of members\n");
  133. topText.setTextFill(Color.RED);
  134. return;
  135. }
  136.  
  137.  
  138. for (Club each : clubList)
  139. {
  140. if (each.getClubName().equalsIgnoreCase(title))
  141. {
  142. topText.setText("Club not added - duplicate\n");
  143. topText.setTextFill(Color.RED);
  144. return;
  145. }
  146. }
  147.  
  148. //creation of club
  149. Club newClub = new Club(title,members,univ);
  150. selectPane.updateClubList(newClub);
  151. area.appendText(newClub.toString());
  152. topText.setText("");
  153. //at the end, don't forget to update the new arrayList
  154. //information on the SelectPanel
  155. //----
  156.  
  157. //}
  158.  
  159. } //end of handle() method
  160. } //end of ButtonHandler class
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement