Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package Assignment6;
  2.  
  3. import com.sun.tools.javac.comp.Check;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.control.CheckBox;
  7. import javafx.scene.control.TextArea;
  8. import javafx.scene.layout.*;
  9. import javafx.event.ActionEvent; //**Need to import
  10. import javafx.event.EventHandler; //**Need to import
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import javafx.collections.ObservableList;
  15. import javafx.scene.Node;
  16.  
  17. //import all other necessary javafx classes here
  18. //----
  19.  
  20. public class SelectPane extends BorderPane
  21. {
  22.  
  23. //instance variables i needed
  24. private ArrayList<Club> clubList;
  25. private TilePane pane;
  26. private Label info,total;
  27. private int totalMembers;
  28. private List<CompareMethod> boxs = new ArrayList<>();
  29. private ArrayList<String> active = new ArrayList<>();
  30.  
  31. //constructor
  32. public SelectPane(ArrayList<Club> list)
  33. {
  34. //initialize instance variables
  35. this.clubList = list;
  36. totalMembers = 0;
  37. this.info = new Label("Select Some Clubs");
  38. this.total = new Label("Total number of members is: " + totalMembers);
  39. this.pane = new TilePane();
  40. this.setTop(info);
  41. this.setLeft(pane);
  42. this.setBottom(total);
  43. } //end of constructor
  44.  
  45. //This method uses the newly added parameter Club object
  46. //to create a CheckBox and add it to a pane created in the constructor
  47. //Such check box needs to be linked to its handler class
  48. public void updateClubList(Club newClub)
  49. {
  50. clubList.add(newClub);
  51. CheckBox box = new CheckBox(newClub.toString());
  52. SelectionHandler handle = new SelectionHandler();
  53. box.setOnAction(handle);
  54. boxs.add(new CompareMethod(box,newClub));
  55. box.setId(newClub.getClubName());
  56. pane.setPrefColumns(boxs.size());
  57. this.pane.getChildren().add(box);
  58. this.setLeft(pane);
  59. }
  60.  
  61. //create a SelectionHandler class
  62. private class SelectionHandler implements EventHandler<ActionEvent>
  63. {
  64. //Override the abstact method handle()
  65. public void handle(ActionEvent event)
  66. {
  67. ObservableList<Node> list = pane.getChildren();
  68. for (int i = 0; i < list.size(); i++)
  69. {
  70. CheckBox box = (CheckBox)list.get(i);
  71. CompareMethod each = null;
  72.  
  73. for (CompareMethod method : boxs) {
  74. if (method.getBox().getId().equalsIgnoreCase(box.getId())) {
  75. each = method;
  76. }
  77. }
  78. if (each == null)
  79. {
  80. continue;
  81. }
  82.  
  83. if (each.getBox().getId().equalsIgnoreCase(box.getId()))
  84. {
  85. if (each.getBox().isSelected())
  86. {
  87. if (!(active.contains(each.getBox().getId())))
  88. {
  89. totalMembers += each.getClub().getNumberOfMembers();
  90. active.add(each.getBox().getId());
  91. } else {
  92. continue;
  93. }
  94. } else {
  95. if (active.contains(each.getBox().getId()))
  96. {
  97. totalMembers -= each.getClub().getNumberOfMembers();
  98. active.remove(each.getBox().getId());
  99. }
  100. }
  101. }
  102. }
  103. total.setText("Total number of members is: " + totalMembers);
  104.  
  105. }
  106. } //end of SelectHandler class
  107.  
  108.  
  109. //class to allow me to compare club to the checkbox to get amount of members
  110. private class CompareMethod
  111. {
  112. private CheckBox box;
  113. private Club club;
  114.  
  115. public CompareMethod(CheckBox box, Club club)
  116. {
  117. this.box = box;
  118. this.club = club;
  119. }
  120.  
  121. public CheckBox getBox()
  122. {
  123. return box;
  124. }
  125.  
  126. public Club getClub()
  127. {
  128. return club;
  129. }
  130.  
  131. }
  132. } //end of SelectPane class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement