• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why can't I do .set...Anchor on the anchor pain? (JavaFx)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a window with the Scenebuilder.

In the controller class I called my anchor-panel which I created in the Scenebuilder., I saved the anchor pane in a variable, the name of the variable is called anchorpane1,

First I wanted to add a shape to it, which I created in the controller class, for that I wanted to do:
anchorpane1.getchildren().add() and in add then my shape purely, that is correct right?

But my next problem, I wanted to set anchors to the shape, that is possible with .setTopAnchor, .setRigthAnchor, .set.LeftAnchor and .setBottomAnchor.

But when I take my anchor panel, I can't do .set...Anchor, why? That does not work
 
Bartender
Posts: 5567
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bwefoi,

welcome to the Ranch and enjoy the stay!

Without seeing any code, it is hard to tell what is going wrong. What I can say for now is that the method 'setXXXAnchor' is a static method, and therefore you should use something like:
 
bwefoi oiwej
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi bwefoi,

welcome to the Ranch and enjoy the stay!

Without seeing any code, it is hard to tell what is going wrong. What I can say for now is that the method 'setXXXAnchor' is a static method, and therefore you should use something like:




Thank you my code is:
 
Piet Souris
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it DOES work when you type

however, in the IDE that I use (NetBeans 12.5), when I type

then this method does NOT show up. It does when I type

Here is an example:

People in the know at this site always stress that you should call static methods with 'Class.method' and not with 'instance.static method'.
 
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to our FX forum.

And welcome to the Ranch (again).
 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet is correct about using the AnchorPane.setTopAnchor() static method reference.

You CAN access the static method on AnchorPane via a AnchorPane instance reference, but it's considered a bad practice.
I would say for the following reasons (at least):
-It harms readability (it looks like calling a regular instance method so it's misleading).
-It's more fragile - you're required to have an instance reference. If the AnchorPane reference you're calling the method on is null, you'll get an NPE. A static method reference won't NPE like that (unless you pass a null parameter maybe).

Hopefully your IDE warns you - don't ignore warnings. IntelliJ's warning about this:


So use this form:
AnchorPane.setTopAnchor(someChildComponentInstance, 10.0);

So:


There's a few other times you use these static methods in JFX too, notably VBox.setVgrow() and HBox.setHgrow(), and I think a few others. They're really important for doing anything other than trivial layouts in JFX. Although if you use SceneBuilder (strongly suggested), you rarely need to worry about these.
 
Did you miss me? Did you miss this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic