How to limit the amount of scrolling ScrollPane does - java

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.

Related

Java How to get JComponent to resize based on frame width

So my calculator program looks good unless I resize it. Then things get all out of whack. Right now I'm using a GridBagLayout to organize my buttons (thought it was best to use GridBag for this situation given a number of buttons). I set the preferredSize to what I would like the size of the buttons to be upon startup. How can I get the buttons to change size upon window resizing?
That's what happens after I resize the app smaller.
When I make it bigger, everything just stays the same and I get a grey emptiness on either side to make up space, instead of enlarging the buttons.
For my buttons dimensions i'm using new Dimension(80, 55)
Then setting the preferredSize to that dimension.
For the memory buttons and jlabels I compute some math to make it as wide as the total buttons across.
Please Note I'm still learning java, as well as the swing library. END NOTE
UPDATE I tried setting the minimum and maximum size when I set the preferred size and still no go END UPDATE
UPDATE 2 I tried setting the size of my main panel to match the size of the app upon start and that didn't work END UPDATE
EDIT 2 My hierarchy is like this:
FRAME
MAINPANEL - Contains all three panels
TOPPANEL - Contains the display
MIDDLEPANEL - Contains memory buttons
BOTTOMPANEL - Contains calculator buttons
END EDIT 2
I recommend using TableLayout from Oracle (you can download it there), because it is more comfortable. Sure it is possible with the GridBagLayout too, but especially when you're new, the TableLayout is much easier to understand and to configure.
double size[][] =
{{0.25, 0.25, 0.25, 0.25}, //Horizontal
{50, TableLayout.FILL, 40, 40, 40}}; //Vertical
frame.setLayout (new TableLayout(size));
It's basically a Grid which is separated into rows and columns. So, each of the cells have an id, for example "0, 0".
The height and width is adjustable with pixels, percentage, and Layout.FILL, which uses the available space. if you are using multiple Layout.FILL, it will separate the available space between them.
To add Buttons or other elements, you will need to know the cell coordinates, if it's bigger than one cell, you can add an end cell to, so you can use the whole place available.
// Add buttons
frame.add (button[0], "1, 1, 4, 1"); // Top
frame.add (button[1], "1, 4, 4, 4"); // Bottom
frame.add (button[2], "1, 3 "); // Left
frame.add (button[3], "4, 3 "); // Right
frame.add (button[4], "3, 3, c, c"); // Center
frame.add (button[5], "3, 3, 3, 4"); // Overlap
Here some useful links:
http://www.oracle.com/technetwork/java/tablelayout-141489.html
http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html
http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Preferred.html

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

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.

Vaadin incorrect alignment

I have the following piece of code that I wrote. I am trying to center both the texts one after another in the panel like this:
Your name is:
1
Can anyone tell me how to go about doing this?
Thank you.
Code:
VerticalLayout layout1 = new VerticalLayout();
layout.setSizeFull();
layout1.setSizeFull();
Label label = new Label("Your name is: ");
label.setStyleName(Runo.LABEL_H1);
Label label1 = new Label(name);
label1.setStyleName(Runo.LABEL_H1);
layout1.addComponent(label);
layout1.addComponent(label1);
layout1.setComponentAlignment(label, Alignment.MIDDLE_CENTER);
layout1.setComponentAlignment(label1, Alignment.MIDDLE_CENTER);
layout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setContent(layout);
panel.setContent(layout1);
Output:
Labels in Vaadin have a default width of 100%, meaning the Label component fills the entire horizontal space, leaving no room for alignment. The reason for this default is that in most cases you'll want the text to wrap once it reaches the edge of the parent component.
To center the labels in your code, just set their widths to undefined by doing label.setWidth(null);
The reson for the extra vertical space is in the VerticalLayout. Since you've set an expicit height for it but haven't specified any expand ratios for the slots, it's making both of the slots equally high; half the height of the layout each. To get around that, you can do one of two things: get rid of the height in layout1 by replacing the call to layout1.setSizeFull(); with layout1.setWidth("100%");—or set an expand ratio for the second slot by doing layout1.setExpandRatio(label1, 1);. Setting the expand ratio will cause the layout to use all the extra space in the second slot, leaving the two labels closer together.
With setExpandRatio you'll of course also have to change the alignment of label1 from MIDDLE_CENTER to TOP_CENTER, so that the empty space is placed below the label and not evenly around it.

How to prevent MigLayout from exceeding a container's bounds

I'm trying to construct a simple status panel using MigLayout as follows:
setLayout(new MigLayout("fillx", "[][p]")); // removing constructor args makes no difference
add(createStatusLabel(), "span 2, wrap");
add(createProgressBar(), "growx, pushx");
add(createCancelButton(), "");
This works fine as long as the status message displayed by the status label is short enough to fit within the current panel's size (the cancel button remains right-justified, and the progress bar resizes to take up the remaining space). If the status message is too long, it is not cropped, and causes the area to exceed the bounds of the container, resulting in the cancel button being pushed off screen.
Any suggestions on how to prevent this from happening?
Thanks
Try setting the maximum width of the label to 100%.
You can do this by changing the layout for the label to "span 2, wrap, wmax 100%"
In my tests, I found that it still didn't look quite right, so you may want to subtract a little bit of length (something like wmax 100% - 10px) to bring it away from the edge.

Categories