JavaFX: Get visible area of ScrollPane - java

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();

Related

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.

Displaying an "overlay" node in JavaFx ScrollPane

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.

Java swing GridLayout adjusting cellsize

After starting a JPanel using GridLayout(4,4) i insert a JLabel (and attach an imageicon to it) inside every grid cell with size of (150,150).
when i resize the JLabel to size (100,100) the image get cropped (which is perfectly fine by me), but i get a wierd looking grid (imaged added at the end).
if this helps: i dont actually resize the window, i just need to make sure the the size of the JLabel is set to (100,100) always, no metter what is the original image size.
before:
http://postimg.org/image/iolyeb8e7/
after:
http://postimg.org/image/5j6g87ein/
thanks
Unfortunately you did not say what you expect the grid to look like. I assume you don't want the cells to be so far apart from each other.
The GridLayout documentation states that...
The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
If you shrink the size of each JLabel (i.e. the components in each of those rectangles) you just do that. You shrink the size of the component, not that of the rectangle. The grid does not care if the component is to small to fill the whole rectangle. At the moment you add the component to the grid1, the grid tries to set the components size to best fit the available space. But if you later change the labels size, the grid does not care.
What you probably want is to change the size of the whole grid: If you set the grids size to 400 by 400 it should evenly divide it to all 4 rows and 4 columns, so you get rectangles of size 100 by 100. All labels should automatically be sized accordingly.
1 Probably it is not exactly while adding the labels but while validating the container, but I don't know all the internal details about how and when layouts do there magic.

JTable in JScrollPane fixed size with resizeable JFrame?

I have a JTable inside of a JScrollPane. I want to get the columns to stay fixed when I resize it. The rows stay the same size, and there is a scrollbar to move up and down. I can't get the scrollbar to work the same way on the vertical though.
Here is a picture of my project, where the y axis of Duke is perfectly normal, and has a scrollbar to scroll to the bottom of the image, the horizontal part is clearly messed up, and should not have expanded that far.
Also, if the frame was made smaller horizontally, there should be a scrollbar just like the vertical.
So my question basically ends up like this; How do you fix the size of a JTable to not resize within the JScrollPane, and then if it's too large for it, display scroll bars.
Btw, Each cell has an image, making up the big image.
Set the table's auto resize mode to "off"
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Take a look at JTable#setAutoResizeMode for more details.
Update
I should mention, this will mean you will become responsible for determine the size of each column.
Take a look at How to use tables and Setting and Changing Column Widths

Show only a portion of an image with java

If I have a large image that is made up of 25 x 25 smaller images in a grid. How can I use java to only show a portion of that larger grid (such as drawing a portion that starts at 125,25 and ends showing at 150,50)?
I'd break up the image into smaller images, put the smaller image cells into their own ImageIcons and then display whichever Icons I desired in JLabels, perhaps several of them. BufferedImage#getSubimage(...) can help you break the big image into smaller ones.
(decided to make it an answer)
If you don't need a physical copy of the sub image and only need to display it then you could add the image to a JLabel which you add to a JScrollPane without any scrollbars. Set the preferredSize() of the scrollpane equal to the dimension of your sub images (25x25). Then you can use
scrollPane.getViewport().setViewPosition(...);
to position the viewport to disply any sub image.

Categories