0

I am creating a simple application in javafx. The main screen has two tabs and each tab has a button. When (button)btn2 in tab2 is clicked, a rectangle should be displayed on the scene. Here is my code. Main class:

public class check extends Application 
{ 

 public Stage primaryStage;
 private BorderPane rootLayout;
  @Override
  public void start(Stage primaryStage) 
  {

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("TabsExample");

        RootLayout();


  }   

      public void RootLayout() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(check.class.getResource("/view/tabs.fxml"));
                rootLayout = (BorderPane) loader.load();

                primaryStage.setScene(new Scene(rootLayout, 500, 500));
                primaryStage.show();
                control controller = loader.getController();



            } catch (IOException e) {
                e.printStackTrace();
            }

        }

      public Stage getPrimaryStage() {
            return primaryStage;
        }

        public static void main(String[] args) {
            launch(args);

        }

    }

Control.java

 public class control  {    

    @FXML

    private Tab tab1;
    @FXML
    private Tab tab2;
    @FXML
    private Button btn1;
    @FXML
    private Button btn2;
    @FXML
    private AnchorPane A2;
    @FXML
    private AnchorPane A1;




    public control(){}
    private check mainclass;

             @FXML public void handleSubmitButtonAction(ActionEvent event) 
             {

                 System.out.println("button clicked");  
                Rectangle r = new Rectangle();
                 r.setX(50);
                 r.setY(50);
                 r.setWidth(200);
                 r.setHeight(100);
                 r.setArcWidth(20);
                 r.setArcHeight(20);

                 A2.getChildren().add(r);

                 }

              public void setcheck(check ch) {
                    this.mainclass = ch;
             }
        }

****FXML:****

    <BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.control">
       <top>
          <TabPane prefHeight="1754.0" prefWidth="1374.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
            <tabs>
              <Tab id="tab1" text="Draw Circle">
                   <content>
                      <AnchorPane prefHeight="200.0" prefWidth="200.0">
                         <children>
                            <Button id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
                         </children>
                      </AnchorPane>
                   </content></Tab>
              <Tab id="tab2" text="Draw Rectangle">
                <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                         <children>
                            <Button id="btn2" layoutX="20.0" layoutY="58.0" mnemonicParsing="false" onAction="#handleSubmitButtonAction" prefHeight="15.0" prefWidth="77.0" text="circle" />

                         </children></AnchorPane>
                </content>
              </Tab>
            </tabs>
          </TabPane>
       </top>
    </BorderPane>

I apologize for a very long question. With this code, the button action listener is invoked and the message "Button clicked" is printed on the console. But, I do not understand why the rectangle is not drawn on the screen.

1 Answers1

0

It looks like you're missing the ID for the AnchorPane in your FXML. Also, the appropriate tag to use here is "fx:id". You can read a bit more about that here: What's the difference between fx:id and id: in JavaFX?

Below is the corrected FXML

<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.control">
   <top>
      <TabPane prefHeight="1754.0" prefWidth="1374.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab fx:id="tab1" text="Draw Circle">
               <content>
                  <AnchorPane fx:id="A1" prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
                     </children>
                  </AnchorPane>
               </content></Tab>
          <Tab fx:id="tab2" text="Draw Rectangle">
            <content>
              <AnchorPane fx:id="A2" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="btn2" layoutX="20.0" layoutY="58.0" mnemonicParsing="false" onAction="#handleSubmitButtonAction" prefHeight="15.0" prefWidth="77.0" text="circle" />

                     </children></AnchorPane>
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </top>
</BorderPane>
Community
  • 1
  • 1
Justin
  • 1,972
  • 13
  • 28
  • Thanks for the reply. I added the ids to both the anchor panes. Now I am getting a null pointer exception.Trace: ... 52 more Caused by: java.lang.NullPointerException at view.control.handleSubmitButtonAction(control.java:61) ... 61 more.. Line 61 traces to A2.getChildren().add(r); – datamining_beginner Oct 12 '14 at 16:48
  • Sorry, the appropriate tag is "fx:id". Edited answer. You may need to change the other ID tags as well. See [What's the difference between fx:id and id: in JavaFX?](http://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx) – Justin Oct 12 '14 at 16:52
  • Thanks a ton Justin. I appreciate it. I am a beginner trying to learn javafx. can you please explain the difference between fx:id and id tag – datamining_beginner Oct 12 '14 at 16:55
  • Got the answer in the link you provided. – datamining_beginner Oct 12 '14 at 16:57
  • I've edited the answer to include all the corrected tags, as well as a link to that SO question explaining it :) – Justin Oct 12 '14 at 16:58