title: Attempting to add more buttons dynamically to my JavaFXML project
body: <p>The coursework requires us to create a graph, which displays RESTFUL data from a server in JavaFX. The file I have is one we recieved from the tutor, who never really explained how he achieved the finished result. So I am just wondering how, within this Java Fxml project, I code the controller to add dynamic buttons? </p>
  
  <p>The buttons that are called already are check boxes, when they are selected they display cars that are for sale in the bar chart. I would like to add buttons that let me choose the year of sales, and also add functionality to hard coded buttons which switch from a bar chart to a line graph.</p>
  
  <p><strong><em>FXML Code</em></strong></p>
  
  <pre><code>&lt;AnchorPane id="" fx:id="AnchorPane1" prefHeight="-1.0"    prefWidth="-1.0"    xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="salescwck3.SalesController"&gt;
  
  &lt;children&gt;
    &lt;VBox spacing="10.0"&gt;
       &lt;padding&gt;
          &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
       &lt;/padding&gt;
       &lt;children&gt;
          &lt;HBox fx:id="HBox1" spacing="10.0"&gt;
             &lt;padding&gt;
                &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
             &lt;/padding&gt;
          &lt;/HBox&gt;
          &lt;HBox fx:id="HBox11" spacing="10.0"&gt;
             &lt;padding&gt;
                &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
             &lt;/padding&gt;
          &lt;/HBox&gt;
          &lt;HBox alignment="TOP_RIGHT" prefHeight="19.0" prefWidth="217.0" spacing="10.0"&gt;
             &lt;padding&gt;
                &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
             &lt;/padding&gt;
             &lt;children&gt;
                &lt;RadioButton fx:id="RadioButton1" mnemonicParsing="false" text="Bar Chart" /&gt;
                &lt;RadioButton fx:id="RadioButton2" mnemonicParsing="false" text="Line Graph" /&gt;
             &lt;/children&gt;
          &lt;/HBox&gt;
          &lt;BarChart fx:id="BarChart1" animated="false" title="Vehicle Sales"&gt;
            &lt;xAxis&gt;
              &lt;CategoryAxis label="Region" side="BOTTOM" fx:id="xAxis" /&gt;
            &lt;/xAxis&gt;
            &lt;yAxis&gt;
              &lt;NumberAxis fx:id="yAxis" label="Quantity of Sales"   side="LEFT" /&gt;
            &lt;/yAxis&gt;
          &lt;/BarChart&gt;
       &lt;/children&gt;
    &lt;/VBox&gt;
   &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p><strong><em>Main Java</em></strong></p>
  
  <pre><code>package salescwck3;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class SalesCwck3 extends Application {    
  @Override
  public void start(Stage primaryStage) throws Exception {
      Parent root =    FXMLLoader.load(getClass().getResource("SalesController.fxml"));
  
      Scene scene = new Scene(root);
        scene.getStylesheets().add(SalesCwck3.class.getResource("Style.css").toExternalForm());
  
      primaryStage.setScene(scene);
      primaryStage.setTitle("SalesCwck3");
      primaryStage.show();
  }
  
  public static void main(String[] args) {
      launch(args);
  }    
  }
  </code></pre>
  
  <p><strong><em>FXML Controller</em></strong></p>
  
  <pre><code>public class SalesController implements Initializable {
  private static String markup;
  private static List&lt;Sales&gt; sales;
  private static List&lt;String&gt; vehicles;
  private static List&lt;Integer&gt; year;
  private DashService service;
  
  private CheckBox[] checkBoxes;
  
  private ChoiceBox[] choiceBoxes;
  
  @FXML
  private RadioButton RadioButton1, RadioButton2;    
  //Need to learn how to switch from bar chart to line graph
  
  @FXML
  private AnchorPane AnchorPane1;
  
  @FXML
  private HBox HBox1;
  //Contains The Check Boxes constructed for vehicles
  
  @FXML
  private HBox HBox2;
  //Populate this with the year Choice Box
  
  @FXML
  private NumberAxis yAxis;
  
  @FXML
  private CategoryAxis xAxis;
  
  @FXML
  private BarChart&lt;?, ?&gt; BarChart1;
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      service = new DashService();
      service.setAddress("http://glynserver.cms.livjm.ac.uk/DashService/SalesGetSales");
      service.setOnSucceeded(new EventHandler&lt;WorkerStateEvent&gt;() {
          @Override
          public void handle(WorkerStateEvent e) {
              markup = e.getSource().getValue().toString();                
              sales = (new Gson()).fromJson(markup, new TypeToken&lt;LinkedList&lt;Sales&gt;&gt;() {}.getType());
  
  
              vehicles = sales.stream().map(o -&gt; o.getVehicle()).distinct().collect(Collectors.toList());
              year = sales.stream().map(o -&gt; o.getYear()).distinct().collect(Collectors.toList());
  
              // ToDo : Debugging !
              System.out.println(markup);
  
              for (Sales sale : sales) {
                  System.out.println(sale.toString());
              }           
  
              constructCheckBoxes();
  
  
          }
      });
  
  
  
      service.start();
  }       
  
  //Need a Progress Loader.
  
  
  
  private void constructCheckBoxes() { 
      checkBoxes = new CheckBox[vehicles.size()];
  
      for (byte index = 0; index &lt; vehicles.size(); index++) {
          checkBoxes[index] = new CheckBox(vehicles.get(index));
          checkBoxes[index].setSelected(true);
          checkBoxes[index].setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent e) {                    
  
                  constructSeries();
              }
          });
  
          HBox1.getChildren().add(checkBoxes[index]);
      }
  
      // NOTE : More Controls Visible !
      AnchorPane1.getScene().getWindow().sizeToScene();
  
      constructSeries();
  }
  
  private void constructSeries() {
      BarChart1.getData().clear();
  
      for (CheckBox checkBox : checkBoxes) {
          if (checkBox.isSelected()) {
              XYChart.Series series = new XYChart.Series();
              series.setName(checkBox.getText());
  
              // NOTE : Java SE 7 Code !
              for (Sales sale : sales) {
                  if (sale.getVehicle().equals(checkBox.getText())) {
                      series.getData().add(new XYChart.Data&lt;&gt;( sale.getRegion(), sale.getQuantity()));
                  }
              }
  
              BarChart1.getData().add(series);
          }
      }
  }
  
  /*
  private void constructChoiceBoxes(){}
  //Trying to create another choice box in order to choose which date of sales are shown on the graph.
  //Need to know where to put the "constructChoiceBox()" and "constructChoiceSeries()" Code for year Choice Boxes.
  private void constructChoiceSeries(){}
  */
  
  
  
  private static class DashService extends Service&lt;String&gt; {
      private StringProperty address = new SimpleStringProperty();
  
      public final void setAddress(String address) {
          this.address.set(address);
      }
  
      public final String getAddress() {
          return address.get();
      }
  
      public final StringProperty addressProperty() {
         return address;
      }
  
      @Override
      protected Task&lt;String&gt; createTask() {
          return new Task&lt;String&gt;() {
              private URL url;
              private HttpURLConnection connect;
              private String markup = "";
  
              @Override
              protected String call() {
                  try {
                      url = new URL(getAddress());
                      connect = (HttpURLConnection)url.openConnection();
                      connect.setRequestMethod("GET");
                      connect.setRequestProperty("Accept", "application/json");
                      connect.setRequestProperty("Content-Type", "application/json");                        
  
                      markup = (new BufferedReader(new InputStreamReader(connect.getInputStream()))).readLine();
                  }
                  catch (Exception e) {
                      e.printStackTrace();
                  }
                  finally {
                      if (connect != null) {
                          connect.disconnect();
                      }
                  }
  
                  return markup;
              }
          };
      }
  }
  }
  </code></pre>
  
  <p><strong><em>Gson Java</em></strong></p>
  
  <pre><code>public class Sales {
  final private int Year, QTR, Quantity; 
  final private String Region, Vehicle;
  
  public Sales(Integer Year, String Region, Integer QTR, String Vehicle, Integer Quantity) {
      this.Year = Year;
      this.Region = Region;
      this.QTR = QTR;
      this.Vehicle = Vehicle; 
      this.Quantity = Quantity;         
  }
  
  @Override
  public String toString() {
      return String.format("%s%s%s%s%s", ("Year:" + Year + " "), ("Region:" + Region + " "), ("QTR:" + QTR + " "),("Vehicle:" + Vehicle + " "), ("Quantity:" + Quantity + " "));
  }
  public Integer getYear() {
      return Year;
  }
  public String getRegion() {
      return Region;
  }
  public Integer getQTR() {
      return QTR;
  }
  public String getVehicle() {
      return Vehicle;
  }
  public Integer getQuantity() {
      return Quantity;
  }
  }
  </code></pre>