title: Javafx good programming practices
body: <p>The majority of my programming experience is in C++. I'm learning a lot of great features in Javafx but my biggest hurdle so far is whether or not I'm using good programming practices when writing applications in Javafx. Primarily, I was taught to keep the "main function" as clean as possible in C++, so it primarily contained my own class objects and their methods. Is this good practice in general when working in fx? i.e creating a number of classes outside of my main class, which contains the start method, and then adding the objects of those classes into the start method? Here is an example:</p>
  
  <p>This program creates a BorderPane and places an HBox with two buttons at the top of it. I wrote the program in two different ways. One - all of the methods are in the Main class. Two - I created a separate class, which instantiates the HBox and then that is used in Main instead. </p>
  
  <p>1.</p>
  
  <pre><code>public class Main extends Application {
      @Override
      public void start(Stage primaryStage) 
      {
          BorderPane border = new BorderPane();
          HBox hbox = addHBox();
          border.setTop(hbox);
  
          Scene scene = new Scene (border, 540,680);
          primaryStage.setScene(scene);
          primaryStage.show();
  
      }
      public HBox addHBox()
      {
          HBox hb = new HBox();
          hb.setPadding(new Insets(15,12,15,12));
          hb.setSpacing(10);
          hb.setStyle("-fx-background-color: #336699;");
  
          Button btnCurrent = new Button("Current");
          btnCurrent.setPrefSize(100, 20);
          btnCurrent.setStyle("-fx-font: 12 times; -fx-base: #b6e7c9;");
  
          Button btnProjected = new Button("Projected");
          btnProjected.setPrefSize(100,20);
          btnProjected.setStyle("-fx-font: 12 times; -fx-base: #b6e7c9;");
  
          hb.getChildren().addAll(btnCurrent,btnProjected);
          hb.setAlignment(Pos.CENTER);
          return hb;
      }
  
      public static void main(String[] args) 
      {
          launch(args);
      }
  }
  </code></pre>
  
  <p>2.</p>
  
  <p>I created a class Called Box:</p>
  
  <pre><code>public class Box extends HBox{
  
  
  HBox hb = new HBox();
      public Box()
      {
  
          hb.setPadding(new Insets(15,12,15,12));
          hb.setSpacing(10);
          hb.setStyle("-fx-background-color: #336699;");
  
          Button btnCurrent = new Button("Current");
          btnCurrent.setPrefSize(100, 20);
          btnCurrent.setStyle("-fx-font: 12 times; -fx-base: #b6e7c9;");
  
          Button btnProjected = new Button("Projected");
          btnProjected.setPrefSize(100,20);
          btnProjected.setStyle("-fx-font: 12 times; -fx-base: #b6e7c9;");
  
          hb.getChildren().addAll(btnCurrent,btnProjected);
          hb.setAlignment(Pos.CENTER);
  
          getChildren().add(hb);
      }
  
  }
  </code></pre>
  
  <p>And then use it in my main Function:</p>
  
  <pre><code>public class Main extends Application {
      @Override
      public void start(Stage primaryStage) 
      {
          BorderPane border = new BorderPane();
          Box hbox = new Box();
          border.setTop(hbox);
  
          Scene scene = new Scene (border, 540,680);
          primaryStage.setScene(scene);
          primaryStage.show();
  
      }
      public static void main(String[] args) 
      {
          launch(args);
      }
  }
  </code></pre>
  
  <p>That is my primary question, but also, if the second style (Creating your own classes and using them in your main class) is indeed the way to go, using my specific example, it places the HBox in an odd place and doesnt look right at all. What am I doing wrong by having the HBox instantiated in my Box Class' constructor and then adding it to my BorderPane in main?</p>
  
  <p>I know that's a lot but if someone can give me some direction I would greatly appreciate it. Thanks so much!</p>