• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaFX ScrollPane scroll child into viewportbounds programatically

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
This is my fisrt post here, I'm really new to JavaFX, till one week ago I was programming in C# WPF and MAUI. MAUI being new on the cross-platform framework, it has a lot of good and bad things, and I tried to find something else to be more stable. And I've found JavaFX
Now, let's see what is about my desire:
I have a FlowPane wrapped in a ScrollPane. This FlowPane is populated with some custom control and I didn't found a possibility to scroll one of the childs into ScrollPane's viewport bounds when I click the control. In .Net MAUI there is a function of ScrollView which automatically will scroll the control into view and align it to the nearest side (top or bottom for vertical, left or right for horizontal) depending on the nearest side of the control.

This line of code will scroll the label Element to the end of the ScrollView, and the true parameter will start the animation for this job.
Is there such a function? Or is there a simple approach to do that? I don't need animation, I only need to scroll the ScrollPane to bring that control into viewport, to the nearest side.

Best regards.
 
Bartender
Posts: 247
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly, I don't believe there is a "simple" way to do that with those components (especially with a FlowPane involved). ScrollPane has a setVvalue() method you can use to scroll, but knowing where to scroll to would be the tricky part. Theoretically that could be calculated using the height of the child components, but that might not work out especially if you're adding/removing components.

If a TableView or a ListView instead of a ScrollPane/FlowPane would work for you, both of those have a method to scroll to a specific row, so that might be easier for you.
 
Valentin Maties
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the solution, I will post it as soon as possible.
Thanks.
 
Valentin Maties
Greenhorn
Posts: 8
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, this is the solution to ScrollToPosition an element.
In my example, I used only for the vertical scroll, so this could be extended for horizontal as well. My custom control is loaded into a FlowPanel and this FlowPanel is the content of ScrollPane.

First, I created an enum to be easy of use:



In my Custom Control's controller I created these methods:



Scrolling method (no animation included yet):


This works like a charm in my app.
Yes, I know maybe this code could be improved, but I'm really new to JavaFX (I have two weeks of Java codding), so, if there is anyone that can improve this code, please you can write modifications.
 
Lou Hamers
Bartender
Posts: 247
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey if it's working, cool. You might find that if you're adding or removing components dynamically and you're attempting to act on their size, that can be a challenge since that is a moving target. But if you're not doing that, you're probably ok.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic