Advertisement
Guest User

proj

a guest
Feb 26th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.20 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3.  
  4. import javafx.geometry.Pos;
  5. import javafx.scene.control.*;
  6. import javafx.scene.text.*;
  7. import javafx.stage.Stage;
  8. import javafx.scene.layout.*;
  9. import javafx.geometry.*;
  10. import javafx.scene.paint.*;
  11. import javafx.scene.layout.HBox;
  12.  
  13. import javafx.event.ActionEvent; //**Need to import
  14. import javafx.event.EventHandler; //**Need to import
  15.  
  16. import javax.swing.*;
  17.  
  18. //import all other necessary javafx classes here
  19. //----
  20. import javafx.scene.layout.GridPane;
  21. import javafx.geometry.Insets;
  22.  
  23. public class CreatePane extends HBox {
  24. ArrayList<Club> clubList;
  25.  
  26.  
  27. private Label titleLabel, numMembersLabel, theUniversityLabel, errorLabel;
  28. private TextField titleTextField, numMembersTextField, universityTextField;
  29. private Button createClub;
  30. private TextArea whiteTextArea;
  31.  
  32. private SelectPane selectPane;
  33.  
  34.  
  35. //constructor
  36. public CreatePane(ArrayList<Club> list, SelectPane sePane) {
  37. this.clubList = list;
  38. this.selectPane = sePane;
  39.  
  40.  
  41.  
  42. //initialize each instance variable (textfields, labels, textarea, button, etc.)
  43. //and set up the layout
  44. //----
  45.  
  46.  
  47.  
  48. errorLabel = new Label("");
  49. errorLabel.setTextFill(Color.RED);
  50.  
  51. titleLabel = new Label("Title ");
  52. numMembersLabel = new Label("Number of Members ");
  53. theUniversityLabel = new Label("University ");
  54. titleTextField = new TextField("");
  55. numMembersTextField = new TextField("");
  56. universityTextField = new TextField("");
  57. createClub = new Button("Create a Club");
  58. whiteTextArea = new TextArea("No Club");
  59.  
  60.  
  61. //create a GridPane hold those labels & text fields.
  62. //you can choose to use .setPadding() or setHgap(), setVgap()
  63. //to control the spacing and gap, etc.
  64. //----
  65.  
  66. GridPane gridpane = new GridPane();
  67. gridpane.setAlignment(Pos.CENTER);
  68.  
  69. gridpane.setHgap(5);
  70. gridpane.setVgap(5);
  71.  
  72.  
  73. gridpane.setPadding(new Insets(10, 10, 10, 10));
  74.  
  75. gridpane.add(errorLabel, 0, 0);
  76.  
  77. gridpane.add(titleLabel, 0, 1);
  78. gridpane.add(numMembersLabel, 0, 2);
  79. gridpane.add(theUniversityLabel, 0, 3);
  80.  
  81.  
  82. gridpane.add(titleTextField, 1, 1);
  83. gridpane.add(numMembersTextField, 1, 2);
  84. gridpane.add(universityTextField, 1, 3);
  85. gridpane.add(createClub, 1, 4);
  86.  
  87.  
  88.  
  89.  
  90.  
  91. //You might need to create a sub pane to hold the button
  92. //----
  93. Pane subpane = new Pane();
  94.  
  95.  
  96. //Set up the layout for the left half of the sample.CreatePane.
  97. //----
  98.  
  99. VBox theVBox = new VBox();
  100.  
  101. theVBox.getChildren().add(gridpane);
  102.  
  103. whiteTextArea.setEditable(false);
  104.  
  105.  
  106. HBox theHBox = new HBox();
  107. theHBox.getChildren().addAll(theVBox, whiteTextArea);
  108. this.getChildren().addAll(theHBox);
  109.  
  110.  
  111. //the right half of the sample.CreatePane is simply a TextArea object
  112. //Note: a ScrollPane will be added to it automatically when there are no
  113. //enough space
  114.  
  115.  
  116. //Add the left half and right half to the sample.CreatePane
  117. //Note: sample.CreatePane extends from HBox
  118. //----
  119.  
  120. //register/link source object with event handler
  121. //----
  122.  
  123. } //end of constructor
  124.  
  125. //Create a ButtonHandler class
  126. //ButtonHandler listens to see if the button "Create" is pushed or not,
  127. //When the event occurs, it get a club's Title, its number of members, and its university
  128. //information from the relevant text fields, then create a new club and add it inside
  129. //the clubList. Meanwhile it will display the club's information inside the text area.
  130. //using the toString method of the sample.Club class.
  131. //It also does error checking in case any of the textfields are empty,
  132. //or a non-numeric value was entered for its number of members
  133. private class ButtonHandler implements EventHandler<ActionEvent> {
  134. //Override the abstact method handle()
  135. public void handle(ActionEvent event) {
  136. //declare any necessary local variables here
  137. //---
  138. createClub.setOnAction(new ButtonHandler());
  139. int count;
  140. Club info = new Club();
  141. Boolean checkForDup = true;
  142.  
  143.  
  144.  
  145. //when a text field is empty and the button is pushed
  146. //if ( //---- )
  147. //{
  148. //handle the case here
  149.  
  150. if (titleLabel.getText().length() == 0 || numMembersTextField.getText().length() == 0 || universityTextField.getText().length() == 0) {
  151.  
  152. errorLabel.setText("Please fill in all the fields");
  153.  
  154.  
  155.  
  156.  
  157. }
  158.  
  159.  
  160. else {
  161.  
  162. try {
  163. String titleNameFromText = titleTextField.getText();
  164. int lengthOfText = Integer.parseInt(numMembersTextField.getText());
  165. String universityNameFromText = universityTextField.getText();
  166.  
  167. for (int i = 0; i < clubList.size(); i++) {
  168. if (clubList.get(i).getClubName().equalsIgnoreCase((info.getClubName()))) {
  169.  
  170. checkForDup = true;
  171. //make a duplicate thing
  172.  
  173. }
  174.  
  175. if (checkForDup == false) {
  176. if (whiteTextArea.getText().equalsIgnoreCase("No clubs")) {
  177. whiteTextArea.clear();
  178. }
  179. clubList.add(info);
  180.  
  181. }
  182. }
  183. }
  184. catch (NumberFormatException check) {
  185. errorLabel.setText("Invalid Input entered");
  186. }
  187.  
  188. titleTextField.setText("");
  189. numMembersTextField.setText("");
  190. universityTextField.setText("");
  191. }
  192. //}
  193. //else //for all other cases
  194. //{
  195. //when a non-numeric value entered for its number of members
  196. //and the button is pushed
  197. //you will need to use try & catch block to catch
  198. //the NumberFormatException
  199. //----
  200.  
  201. //When a club of an existing club name in the list
  202. //was attempted to be added, do not add it to the list
  203. //and display a message "sample.Club not added - duplicate"
  204.  
  205.  
  206. //at the end, don't forget to update the new arrayList
  207. //information on the SelectPanel
  208. //----
  209.  
  210. //}
  211.  
  212. } //end of handle() method
  213. } //end of ButtonHandler class
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement