4 Replies Latest reply: Mar 8, 2013 12:34 PM by David.Warwick RSS

    Trouble finding a place for my Event Handler

    David.Warwick
      I am a newbie working on my first program. I am trying to teach myself JavaFX by creating a simple tic-tac-toe program. I have created an AnchorPane to hold the tic-tac-toe grid which consists of 4 cris-crossing lines that were created in JavaFX Scene builder. There are 2 horizontal lines and 2 vertical lines. In the Java program I loaded my FXML file and my tic-tac-toe grid shows up just fine. I created 9 ImageView's that correspond to the 9 tic-tac-toe squares. I load those with the x.jpg or o.jpg images. Those work fine and I can display them on my grid manually.

      I am having trouble figuring out how to make them show up if I click in one of the squares. I created one rectangle and made it the same color as the grid and with no border and placed it in the middle square. I set an fx:id ="rmm" (rectangle middle-middle) and an onMouseClicked="#handleRMM" in the FXML file.

      I created an event handler and I know it is firing because I set System.out.println("Clicked!"); and I can see it displaying on the NetBeans console. But the rest of the event to display the X is not displaying.

      I can only get the event handler to work in the public class Main Extends Application part of the program. But, the AnchorPane, my ImageViews, and everything else is defined in the public void start(Stage primaryStage) part of the program. Therefor, I can not access the ImageViews from the Event Handler. I will list my code and hopefully someone can tell me what I am doing wrong. Thanks in advance.

      David
      package tictactoe;
       
       
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javafx.application.Application;
      import javafx.fxml.FXML;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.*;
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      import javafx.scene.layout.AnchorPane;
      import javafx.stage.Stage;
       
      /**
       *
       * @author David
       */
      public class Main extends Application {
       
          
          /**
           * @param args the command line arguments
           */
          public void main(String[] args) {
              Application.launch(Main.class, (java.lang.String[])null);
          }
       
          @Override
          public void start(Stage primaryStage) {
              
              
              try {
                              
                 
                  AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("TicTacToe.fxml"));
                  Scene scene = new Scene(page);
                  
                  primaryStage.setScene(scene);
                  scene.getStylesheets().add("tictactoe/tictactoe.css");
                  primaryStage.setTitle("TicTacToe");
                  ImageView mm =new ImageView();//Middle Middle
                  final ImageView lt =new ImageView();//Left Top
                  final ImageView lm =new ImageView();//Left Middle
                  final ImageView lb =new ImageView();//Left Bottom
                  final ImageView tm =new ImageView();//Top Middle
                  final ImageView bm =new ImageView();//Bottom Middle
                  final ImageView rt =new ImageView();//Right Top
                  final ImageView rm =new ImageView();//Right Middle
                  ImageView rb =new ImageView();//Right Bottom
                  //mm.setImage(new Image("tictactoe/images/x.jpg"));
                 
                  //page.getChildren().add(mm);
                  
                  mm.relocate(246,145); mm.setFitWidth(100); mm.setFitHeight(100);
                  lt.relocate(107,35);
                  lm.relocate(107,145);
                  lb.relocate(107,260);
                  tm.relocate(246,35);
                  bm.relocate(246,260);
                  rt.relocate(375,35);
                  rm.relocate(375,145);
                  rb.relocate(375,260);
                  
                  page.getChildren().add(mm);
                  //page.getChildren().remove(mm);
                  page.getChildren().add(lt);
                  page.getChildren().add(lm);
                  page.getChildren().add(lb);
                  page.getChildren().add(tm);
                  page.getChildren().add(bm);
                  page.getChildren().add(rt);
                  page.getChildren().add(rm);
                  page.getChildren().add(rb);
              
                  primaryStage.show();  
                  
              
              }
                 
                                   
           
              
              catch (Exception ex) {
                  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                          }
                  
          
          }
      
           
          public void handleRMM() {
              
             ImageView mm =new ImageView();
              mm.relocate(246,145); mm.setFitWidth(100); mm.setFitHeight(100);
              mm.setImage(new Image("tictactoe/images/x.jpg"));
              
              System.out.println("Clicked!");
          }
          
      }
      and my FXML file:
      <?xml version="1.0" encoding="UTF-8"?>
      
      <?import java.lang.*?>
      <?import java.util.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.image.*?>
      <?import javafx.scene.layout.*?>
      <?import javafx.scene.shape.*?>
      <?scenebuilder-stylesheet tictactoe.css?>
      
      <AnchorPane id="AnchorPane" fx:id="anchor" prefHeight="400.0" prefWidth="600.0" style="" xmlns:fx="http://javafx.com/fxml" fx:controller="tictactoe.Main">
        <children>
          <Line endX="100.0" endY="1.0" layoutX="15.0" layoutY="125.0" startX="450.0" startY="1.0" />
          <Line endX="100.0" endY="0.0" layoutX="15.0" layoutY="250.0" startX="450.0" startY="0.0" />
          <Line endX="-100.0" endY="-125.0" layoutX="301.0" layoutY="158.0" startX="-102.0" startY="180.0" />
          <Line endX="-100.0" endY="-125.0" layoutX="465.0" layoutY="163.0" startX="-100.0" startY="180.0" />
          <Rectangle id="rlt" fx:id="rmm" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="122.50001525878906" layoutX="202.0" layoutY="127.0" onMouseClicked="#handleRMM" stroke="WHITE" strokeType="INSIDE" width="163.4998779296875" />
        </children>
      </AnchorPane>
        • 1. Re: Trouble finding a place for my Event Handler
          jsmith
          Here is a simple tic-tac-toe game, perhaps that will help you:
          https://gist.github.com/jewelsea/5115901
          • 2. Re: Trouble finding a place for my Event Handler
            David.Warwick
            Hi John. Thanks for letting me see the code for your tic-tac-toe game. To be honest, I understood some of it, but most of it is over my head right now. I guess what I need help with is understanding the impact and meaning of public, private, void, static, class, and things like that. My understanding of public is that anything that you declare inside the curly braces is available to the entire program. But I don't understand public void or static and what those are used for and I see those keywords frequently. I believe that is why I am having trouble with my little program. When I try to declare an event handler in the public start section of my program, I get Illegal start of expression error.

            Thanks,

            David
            • 3. Re: Trouble finding a place for my Event Handler
              James_D
              Just my $0.02 worth.

              Any interactive game is a pretty complex starting point. The logic for a game is quite complex (even if it's a really simple game). For Tic-Tac-Toe you'll not only have to process the mouse clicks but you need some way of telling if it's a valid move (i.e. if the square isn't already occupied). That's not hard in itself, but you need to figure out where to keep the data underlying that logic, and really the data (game state) should be independent of the display of the game: in short, on some level, you'll need to understand model-view-controller architecture.

              Right now you're at the stage where you're still trying to understand the language syntax, and you're unlikely to learn that just by trying code and posting to forums. Of course, you may well be looking at other resources too, and there are more resources for learning Java than I can possibly list, but perhaps Oracle's [url http://docs.oracle.com/javase/tutorial/java/index.html]Java tutorial would be a good starting point. Understanding the first three chapters or so will answer the immediate syntax questions you are having.

              If you want other references, Kathy Sierra's "Head First Java" and Bruce Eckel's "Thinking in Java" are ones that consistently get good reviews.
              • 4. Re: Trouble finding a place for my Event Handler
                David.Warwick
                Thanks James. I will keep that in mind. I have read over the JavaFX tutorial, but maybe I should look at the Java Tutorial since JavaFX seems to be an extension of Java.

                David