Vaadin relative sizing with component proportions? - java

I have a GridLayout I'm making which is populated with a bunch of pictures. The GridLayout itself is set to SizeFull(), as is each individual image in the grid.
The grid with the pictures is within another grid, and that grid has relative sizes set.
With this set up, the grid of pictures stays within the spot I want it to, properly resizing to fit within the space they should, but the pictures do not retain their proper square proportions. They squish fat or skinny however they want. I want them to retain the original proportions, though, so they expand to fill their available area as much as possible while retaining those proportions.
If I set the width to 100%, or the height to 100%, and leave the other undefined, then it retains the proportions, and properly expands to fit the one that is set to 100%, but the other spills outside the nested grid layout's spot in the upper grid layout.
Anyone know how to do this?

Have you tried setting the width and height as percentage?
So on an image of 80 by 120 pixels:
setWidth(66, Unit.PERCENTAGE)
setHeight(100, Unit.PERCENTAGE)

Related

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

TextView dp does not scale with screen size

I am attempting to create a textView that will take up a large portion of the top center part of the screen. Ideally, setting the text size in dp would allow the textView to remain the correct relative size (taking up about 70% of the width of the screen, and maybe 20% of the top). It looks correct in the layout editor, and in the emulator at HVGA resolution. However, when I test it at higher resolutions (on my tablet, or emulating a 720p display) the text takes up a very small portion of the top center part of the screen. (maybe 30% width instead of 70%, and it doesn't seem any larger vertically).
Is there a way to scale text to correctly increase relative size with resolution?
Set the layout_width as fill_parent, which will make it spread across the screen for all devices.
Add some padding on left right and top which seems suitable.
The padding might seem different for different screens but still this might be a better solution.
set
android:layout_width="match_parent"
edit:
check this out
Auto Scale TextView Text to Fit within Bounds

Fixed percentage based grid with MiGLayout

I am trying to create a grid with MiGLayout that is enforced on its children. This means that if I insert a child into grid position (1,1) and the grid's size is [10%!] that this child must NOT be bigger and overlap other cells. The child must be shrunk to fit the Grid cell.
This is what I have so far:
new MigPane("", "[5%!][20%!][5%!][65%!][5%!]", "[45%!][50%!][5%!]");
Now, I insert a big component (a picture that I have no control over) in Grid 1,1, like this:
migPane.add(myImageView, "cell 1 1, width 100%!");
However, that does not seem to restrict the ImageView at all.
How do I tell MiGLayout that I want "myImageView" to be put in grid 1,1 and size it to fit? Is there a "fit" keyword? :)
Note that specifying anything with pixels/points/mm/cm/inches is NOT what I want. My app always runs full-screen and must scale seamlessly (it is not a traditional form app, it is a video system using JavaFX).
It looks like percentages are supported, according to the docs:
Overrides the default size of the component that is set by the UI
delegate or by the developer explicitly on the component. The size is
specified as a BoundSize. See the Common Argument Types section above
for an explanation. Note that expressions is supported and you can for
instance set the size for a component with "width pref+10px" to make
it 10 pixels larger than normal or "width max(100, 10%)" to make it
10% of the container's width, but a maximum of 100 pixels.
Maybe try something like: "width max(100%, 100%)".

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