• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaFX : Simply binding and property to draw rectangle

 
m florian
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Firstly, sorry for my bad English.
I have a question to ask you, I try to create a simply application in JavaFX that use property and binding.
I reduce my application at the minimal for show you my problem.

I would like to put a red rectangle at the middle of borderPane, this rectangle is into an HBox (I think the problem is here) and i would like that rectangle grow up or reduce in the same time that is container (the HBox).
This is my code :

But it does'nt work... When I slowly resize my Frame, it's work, nevertheless, when I fastly resize my Frame, the rectangle is not in the middle (not the same size too) and we can see the same things when we minimize and maximize the Frame.

I don't understand why it does'nt work. So if you can help me to explain how do this for a rectangle in HBox thank you.

Thank you for your help,

Sorry for my bad english again.

Florian.
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that there is a time lag between when the bindings are applied and when the layout is calculated.

If you use a resizable pane rather than a rectangle and set appropriate constraints on the pane, so that it behaves and resizes like your rectangle does, then I don't see the same laggy behavior. I think that is because the pane is resized within the same layout pass as the rest of the UI whereas the bound rectangle might be resized in a subsequent layout pass, but that is just speculation.

When you use binding for layout there is a slightly recursive or iterative relationship happening, the HBox's width is calculated based upon its content width (the rectangle's width), but the content width (the rectangle's width) is calculated using a bind to the HBox's width - I'm not sure that specifying such interdependent layout rules using binding is a very good idea. As an alternative you can use resizable layout managers with constraints (as shown in the example below), or layout programatically by overriding the layoutChildren() method for Region. Over-riding layoutChildren() is the way the internal implementation of JavaFX does most of its layouts for the layout managers and controls, however there are many tricks and difficulties to doing this (such as clean handling of snapping to pixel boundaries), so implementing your own layout by over-riding layoutChildren() is an advanced topic for which there is little to no tutorials on the Internet. So I advise sticking to constraints specified using existing resizable layout managers.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic