Skip to main content

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

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
danek101
Offline
Joined: 2008-04-16

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?

AttachmentSize
scrollpane.png23.1 KB
scrollpane2.png24.19 KB
scrollpane3.png26.55 KB