Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.scene.control.Label;
- import javafx.scene.control.CheckBox;
- import javafx.scene.layout.*;
- import javafx.event.ActionEvent; //**Need to import
- import javafx.event.EventHandler; //**Need to import
- import java.util.ArrayList;
- import java.util.List;
- import javafx.collections.ObservableList;
- import javafx.scene.Node;
- public class SelectPane extends BorderPane
- {
- //instance variables
- private ArrayList<Club> clubList;
- private List <CompareMethod> compare = new ArrayList<>();
- private ArrayList <String> list = new ArrayList<>();
- private Label numMembers, title;
- private TilePane newPane;
- private int members;
- //constructor
- public SelectPane(ArrayList<Club> list)
- {
- //initialize instance variables
- this.clubList = list;
- members = 0;
- this.newPane = new TilePane();
- this.title = new Label("Select some clubs");
- this.numMembers = new Label("The total number of members for the selected club(s) is: ");
- //set up the layout
- //----
- this.setBottom(numMembers);
- this.setTop(title);
- this.setLeft(newPane);
- } //end of constructor
- //This method uses the newly added parameter Club object
- //to create a CheckBox and add it to a pane created in the constructor
- //Such check box needs to be linked to its handler class
- public void updateClubList(Club newClub)
- {
- CheckBox checkBox = new CheckBox(newClub.toString());
- SelectionHandler sectionHandler = new SelectionHandler();
- checkBox.setOnAction(sectionHandler);
- compare.add(new CompareMethod(newClub, checkBox));
- checkBox.setId(newClub.getClubName());
- clubList.add(newClub);
- checkBox.setId(newClub.getClubName());
- newPane.setPrefColumns(compare.size());
- this.setLeft(newPane);
- this.newPane.getChildren().add(checkBox);
- }
- //create a SelectionHandler class
- private class SelectionHandler implements EventHandler<ActionEvent>
- {
- //Override the abstact method handle()
- public void handle(ActionEvent event)
- {
- //When any radio button is selected or unselected
- //the total number of members of selected clubs should be updated
- //and displayed using a label.
- ObservableList <Node> newlist = newPane.getChildren();
- for (int i = 0; i < newlist.size(); i++)
- {
- CheckBox createBox = (CheckBox) newlist.get(i);
- CompareMethod each = null;
- for (int j = 0; j < compare.size(); i++)
- {
- CompareMethod testBox = compare.get(j);
- if (testBox.getBox().getId().equalsIgnoreCase(createBox.getId()))
- {
- each = testBox;
- }
- }
- if (each == null)
- {
- continue;
- }
- if (each.getBox().getId().equalsIgnoreCase(createBox.getId()))
- {
- if (each.getBox().isSelected())
- {
- if (!(list.contains(each.getBox().getId())))
- {
- members += each.getClub().getNumberOfMembers();
- list.add(each.getBox().getId());
- } else {
- continue;
- }
- } else {
- if (list.contains(each.getBox().getId()))
- {
- members -= each.getClub().getNumberOfMembers();
- list.remove(each.getBox().getId());
- }
- }
- }
- }
- }
- } //end of SelectHandler class
- private class CompareMethod {
- private Club club;
- private CheckBox box;
- public CompareMethod(Club club, CheckBox box)
- {
- this.club = club;
- this.box = box;
- }
- public CheckBox getBox()
- {
- return box;
- }
- public Club getClub()
- {
- return club;
- }
- }
- } //end of SelectPane class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement