JavaFX How to scroll ScrollPane to have node in the middle of view port?
Asked Answered
C

3

6

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?

0:00 hour 12:00 24:00

Cedrickceevah answered 3/9, 2013 at 9:21 Comment(3)
In a nutshell, the green line represents the current time and you want it to always stay at the same position in the middle of scrollpane while the timeline graph flows from right to left. Is it right?Vicechancellor
Yes, exactly... I just want have this panel under green line to move. ( top axis is bind with it )Cedrickceevah
The basic observation is that (hValue-hMin)/(hMax-hMin) = x / (contentWidth - viewportWidth), where x is the horizontal offset of the left edge of the viewport from the left edge of the content. Then you have centerX = x + viewportWidth/2 => from JamesD's answer to How may I zoom a Node centered in a ScrollPane?.Macedoine
C
0

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?

Chinatown answered 4/12, 2013 at 19:32 Comment(0)
R
0

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.

Resht answered 16/1, 2014 at 3:53 Comment(0)
C
0

This formula worked for me to center my objects in a scrollpane:

scrollPane.setHvalue(line.getX() - scrollPane.getWidth() / 2) / (anchorPane.getWidth() - scrollPane.getWidth()));
Coffeecolored answered 3/3, 2022 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.