JavaFX How to scroll ScrollPane to have a node in the middle of the viewport?

I need to create a timeline with a line showing the current time. I am using AnchorPane with an added line located in a ScrollPane. I need to simulate 1 day, scroll width (2880 pixels, every 60 pixels - one hour). My limits (previous day 12 hours and next day 12 hours). Moving my line is working fine.

All I have to do is set the line in the middle of the viewport and move the content under it. For a moving line, I use this method:

scrollPane.setHvalue( line.getStartX() - anchorPane.width); 

It works "excellent." The content moves, but the line moves. I start when the line is 0:00 (the position is set to 0.25) and ends when the position (0.75) is 24 hours. I also scale my axis of the timeline Y so

 /* where line position is calculate by scale, scale is x2, x3, x4.... */ scrollPane.setHvalue( line.getStartX() - anchorPane.width * scale); 

Please see imgs for several hours: 0:00, 12:00, 24:00 ... The correct line position is only at 12:00 ... How can I modify the function to set the line position?

0:00 hour12:0024:00

+6
source share
2 answers

Is there a reason why the line should be inside scrollpane. Why not just overlay it on top of the entire scroll?

or see this article on using bindings to fix a node inside scrollpane: Is it always displayed when scrolling through a bound node in Java FX 2.0?

0
source

I do not know if I understand your question. It seems to me that you have a scroll pane with a node with a green line on top of it, right? If so...

If the green line is node, perhaps you can register an event handler for the displayed value of the scroll bar (hvalue, I'm belive). For example, every time the scroll value of the scroll area changes, an update must be made of the translation position of the green node line. I am not a specialist in layouts. JavaFX is still a newbie. But from what I know, it seems that this can be done. Theoretically, this, apparently, can be done.

0
source

Source: https://habr.com/ru/post/953040/


All Articles