This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Changing properties of shapes create in Scene Builder from within my Java program

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a program in Java and I am trying to create a UI for it with JavaFX. JavaFX comes with software called Scene Builder which allows you to edit an FXML file which is referenced by Java for creating the UI layout.

In Scene Builder I created a series of circle shapes. Here is a simplified version of the FXML file with just 4 circles:



In order to control the appearance of these circle shapes I need to create variables referencing them in my Controller.Java file. I also put the variable names into a 2D array because I need to reference them from other 2D arrays in my Java program:



There is also a Main.java file that starts up JavaFX and loads in the FXML file:



The problem that I am having, is that I need to store the references to the circles in a 2D Array, because I need to reference and control them from elsewhere in my Java program. I tried putting all of the variable names into a 2D array, but Java sees them as null.

If you run the three files listed above, a window with 4 blue circles will appear. Clicking on the first circle will make the last circle turn red. However, clicking on the second circle will generate a java.lang.NullPointerException error. (If it had worked the way I was hoping, it would have turned the 2nd last circle red).

I was told that I need to initialise the Circle variable, by making it equal to a Circle object instance before placing it into the 2D array. This would get around the problem of null values in the 2D array. But I don't see how I can initialise the Circle values in the Controller.java file because the Circle objects were created in the FXML file and not in the Controller.java file. Is there some way I can store and reference objects that are created in the FXML file?
 
Sheriff
Posts: 6115
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Controller, line 11-13, try this instead:
This injects the circles from your FXML into your Controller.  You may also have to put the Controller into the FXML file, but try this first.
 
Conor O'Boyle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried adding in the code you just posted, to replace what I had on line 13 of Controller.Java but the same thing happened. If I use it works fine, and the fourth circle turns red. But if I use: I get the error message (was hoping that line might turn the 3rd circle red). How do I put the controller into the FXML file?
 
Greenhorn
Posts: 25
1
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sysout circleGrid[1][0] and check what’s in there.
your controller should be hooked up correctly. You can check that in the fxml file, you should find something like fx:controller=.....
 
Conor O'Boyle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The controller is correctly hooked up as far as I can see. I have changed the MouseEvent below method to illustrate my problem:

In the following MouseEvent, If I print circleGrid[1][0] to the console I get null. If I get the source of the button click and insert that into my circleGrid then the object is displayed after I print circleGrid[1][0]:
This outputs the following to the console:
null
Circle[id=nCircle_0_1, centerX=0.0, centerY=0.0, radius=20.0, fill=0x1e90ffff, stroke=0x000000ff, strokeWidth=4.0]

The first time it outputs circleGrid[1][0] it comes out as null. Then if I use getSource to place the object into circleGrid at position [1][0] it outputs the full object to the console if I again print circleGrid[1][0]. So at that point, the object and not the null value exists in circleGrid[1][0]. I can then pass circleGrid[1][0] as a parameter into other methods to change the properties of the circle shape in other ways. So, it is possible to put the full object reference into the 2D array. But the only way I can do it is by clicking on the shape and using getSource. Is there a way I can put all of the object references into my 2D array when Controller.java loads, without having to click on them? At the moment if I set up a for loop to put the object names into the array when Controller.java they just go in as null.
 
Knute Snortum
Sheriff
Posts: 6115
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be curious to know is what printing the variable nCircle_0_0 in btnClick2() would produce.  It would at least establish whether the variables were getting hooked up properly.
 
Conor O'Boyle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the following line into btnClick2() in response to your question:
System.out.println(nCircle_0_0);

It printed the following to the console:
Circle[id=nCircle_0_0, centerX=0.0, centerY=0.0, radius=20.0, fill=0x1e90ffff, stroke=0x000000ff, strokeWidth=1.0]

This was what I expected, because Java is seeing nCircle_0_0 as a circle object. When I refer to the circle object nCircle_0_0 in a command such as: nCircle_0_0.setFill(Color.web("#ed4b00"); it has always worked.

So, when I use nCircle_0_0 in a command it sees it as a circle object. The problem I was having, is that if I insert all of these circle object names into an array after I have declared them at the top of Controller.java, then java sees them all as null. So, then when I try to reference them from the array with btnClick2() with I get an error.

However, just now when I printed out nCircle_0_0 with btnClick2() I noticed a strange thing:

If I add the following two lines into btnClick2()

This prints the following in the console:
Circle[id=nCircle_0_2, centerX=0.0, centerY=0.0, radius=20.0, fill=0x1e90ffff, stroke=0x000000ff, strokeWidth=1.0]

So now, unlike line 15 at the top of Controller.java the full object reference goes into theCircles array, rather than null going in (which is what happened at line 15). So it seems that sometime after line 15 and the point when btnClick2() is triggered, Java stops seeing references like nCircle_0_2 as null, and starts seeing them as the Circle objects in my FXML file. If I could figure out where that point is, then I could use a for loop to put all of the circle object references into an array, and control them from elsewhere in the program. Thanks
 
Conor O'Boyle
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i have figured out what the problem is. I added this method into Controller.java:

If I call this method from within Controller.java, then the full circle references go into theCircles array. However, if I call this method from Main.java then null goes into theCircles array.

Thank you very much for your help. if you hadn't asked me those questions, I wouldn't have figured it out.
 
Knute Snortum
Sheriff
Posts: 6115
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, and thanks for posting your solution.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!