Displaying an "overlay" node in JavaFx ScrollPane - java

I have a problem with a surprisingly simple thing..
I have a large Pane in a scrollpane, and I am trying to display one of its children in the top-left corner of the scrollpane, regardless of where the viewport is scrolled.
I've tried to calculate the position with hValue and vValue of the Scrollpane, but couldn't quite get the result I wanted.
How should I do this?

Blast. I figured it out myself.
Bounds b = container.getBoundsInParent();
node.setX(scroll.getHValue() * (b.getMaxX() - scroll.getWidth()));
node.setY(scroll.getVValue() * (b.getMaxY() - scroll.getHeight()));
... will display the node in the top left corner.

Related

How do I refer to the distance I’ve scrolled in a JScrollPane?

I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.

How to limit the amount of scrolling ScrollPane does

When scrolling on my ScrollPane, it leaves a lot of empty space when I reach the bottom, I want this to be limited so it stops allowing scrolling when the bottom item of the ScrollPane becomes visible.
Image 1 - Top of the ScrollPane
Image 2 - Midway down the ScrollPane
As you can see there's a lot of space at the bottom, ideally this would stop being able to scroll after "TestIdleLevel2" becomes visible.
ScrollPane:
ScrollPane itemCollectionScrollPane = new ScrollPane(itemCollection);
itemCollectionScrollPane.setPrefSize(shopInterface.getPrefWidth()*1.55, shopInterface.getPrefHeight());
itemCollectionScrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
itemCollectionScrollPane.getStylesheets().add(Shop.class.getResource("/cssFolder/TransparentScrollPane.css").toExternalForm());
Transforms for the items within it:
itemBox.getTransforms().add(new Translate(shopInterface.getTranslateX() - itemBox.getPrefWidth() / 8,
shopInterface.getTranslateY() + (counter * shopInterface.getPrefHeight() / 3), 0));
itemCollection.getChildren().addAll(itemBox);
Each transform is separated by a counter to space it out, this stops after the items have been looped through (4).
The preferred height is set to about 300, and should be extended to about 400 due to the ScrollPane. However, the ScrollPane is extending it to around 500-600.
Issue: itemCollection was being initialized as the same size as shopInterface.
Solution: Initialize the height as a smaller instance of shopInterface. (In this case the exact amount was shopInterface.getHeight()/4).
Special thanks to Fabian.

JavaFX: Get visible area of ScrollPane

What I want to do:
I've got a JavaFX ScrollPane and I need to determine the area visible in the ScrollPane. I know about ScrollPane.getViewPortBounds(), which allows me to get the size of the visible area, but not the position.
Is there any way I can do this?
In context:
I'm displaying a very large image, which needs to be displayed in only portions at a time. The entire ScrollPane is used to scroll the image, but to determine which parts of the image I need to load, I need to know the visible area displayed in the ScrollPane.
ScrollPane also provides the visible area. This code seems to work:
Bounds bounds = scrollPane.getViewportBounds();
int lowestXPixelShown = -1 * (int)bounds.getMinX() + 1;
int highestXPixelShown = -1 * (int)bounds.getMinX() + (int)bounds.getMaxX();

Finding on-screen region in JavaFX

Seems like this should be easy and obvious but I can't find the answer anywhere. I'm using JavaFX 2 and I need to find the width and height of the region of a node that is currently being displayed on screen to the user (NOT the width and height of the node in the whole layout).
So if the window is scrolled so that my node is halfway off the edge to the right, I'd expect to get a displayed width of half the node's width and a displayed height equal to the full height. Should be easy; isn't. Anyone know how it can be done?
A half-acceptable solution for me would be to use the dimensions of the window itself. Is this the same as the width / height of the Stage?
Thanks
Isn't this a simple calculation using the viewport dimensions of the scrollpane and the coordinates/dimensions of your node?
It would be easierto help if you could post a simple testcase to experiment with.

Limit JScrollPane to only show part of its content

I have a JScrollPane that contains a JPanel of size 5000x5000 pixels. I want to limit the JScrollPane to only be able to show a subpart of this JPanel.
For example the rectangle defined by the two points (X,Y):
(500,500) (upper left corner)
(3000,3000) (lower right corner).
I have tried
myJScrollPane.getHorizontalScrollBar().setMinimum(500);
and
myJScrollPane.getHorizontalScrollBar().setMaximum(3000);
But it doesn't work. The setMinimum function only sets the position of the viewport on the scrollpane, setMaximum doesn't do anything.
Change your JPanel instead to have size (2500, 2500) and render desired fragment of the image. Use translate() of the Graphics in paintComponent() for this.

Categories