Reputation: 634
So my program creates a MigLayout GUI and has a bunch of JLabels that are given images on creation. Here you can see what the final result is right now:
The images are too big and go over the size of the JPanel they're contained in. It is simply creating it to be the size that the original image was and then display that, however, it's obviously an issue. Is there a way I can fix the size so it will try to fit every component in the GUI?
For starters, I want to ensure that the blue portion does not get shifted like that, I want it to keep to the size it was set as (half the height). I also want the images to take up the maximum size they can with keeping the aspect ratio of the original image.
I've tried setting the JLabel's size and preferred size, I've tried some attempts with MigLayout giving it paramaters when I tell it to add the JLabel, neither have worked for me. I'm entertaining the idea of extending JLabel and writing something to fix it, but not entirely sure how to go about that. I know I could overwrite paint but not sure what I'd have to actually do.
My JPanel is set as follows:
JPanel jpCards = new JPanel(new MigLayout("insets 0, gapx 0, gapy 0, wrap 7", "grow"));
My cards are being added without additional parameters and created just by giving it pictures of the size they were displayed.
Just to clarify, I need the actual JLabel's size to be changed, not just what's contained inside, because they will have mouse listeners later on. If only the image changed, I'll have problems with the mouse listener detecting itself inside a JLabel's that it doesn't look as if it should
Upvotes: 2
Views: 635
Reputation: 634
I solved this by instead changing the size of the icon before making the JLabel.
I've found that in Java, when you create a JLabel given some sort of image, it will fit the size of the JLabel to be that of the image you give it. So instead of creating the JLabel given the original image, I scale it first using the dimensions of the screen, then create a JLabel out of it.
There's still some work to be done since I want the user to be able to resize the screen and have the image resize. I'll have to spend some time figuring out how I can get it to resize, but it's not a major concern.
Upvotes: 1