Advertisement
Guest User

SelectPane

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