JavaFX How to scroll ScrollPane to have node in th

2019-06-19 02:51发布

问题:

I have to create timeline with line showing current time. I'm using AnchorPane with added line placed in ScrollPane. I need to simulate 1 day, scrollpane width is ( 2880px, every 60px is one hour). My limits are ( previous day 12hour, and next day 12 hour). Moving my line is working fine.

All I need to do is to set line in the middle of view port and move content under it. For moving line i'm using this method:

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

It is working "kind of fine". Content is moving but line is moving to. I'm starting when the line is 0:00 ( position is set to 0.25 ) and finishing when position is ( 0.75 ) 24 h. I'm also scaling my time line axis 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 hours: 0:00, 12:00, 24:00... Correct line position is only at 12:00... How to modificate function to set position of the line?

回答1:

Is there a reason the line has to be inside the scrollpane. Why not just overlay it over the entire scrollpane?

or see this article about using binding to fix a node inside a scrollpane: Always visible when scrolled anchored Node in Java FX 2.0?



回答2:

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

If the green line is a node, maybe you can register an event handler for the display value of the scroll pane (hvalue, i belive). For example, every time the scroll value of the scroll pane is modified, an update on the translational position of the Green Line node should be made. I'm no expert on layouts. JavaFX is still new to me. But from what I know, it seems it can be done. In theory, this can apparently be done.