Reputation: 15661
The size of the JLabel is larger than the size of the image. With the code below, the border is created around the JLabel and not around the image. How can I created border on the image and Not on the JLabel?
ImageIcon icon;
Border border = BorderFactory.createLineBorder(Color.RED,5);
Image image = icon.getImage().getScaledInstance(widthX,heightY, Image.SCALE_SMOOTH);
icon.setImage(image);
JLabel label = new JLabel(icon);
label.setBorder(border);
Upvotes: 1
Views: 4856
Reputation: 11
Use labelname.setBorder(BorderFactory.createLineBorder(color.BLACK)).
Upvotes: 0
Reputation: 806
You could create an implementation of the Icon
interface which accepts a border thickness and a ImageIcon
to which image drawing is delegated.
Upvotes: 0
Reputation: 324118
The size of the JLabel is larger than the size of the image.
Why? Is it a problem with your layout manager? Or is this a wierd requirement.
You don't appear to be using any text, just an image, so I would just add the label to another panel that uses a FlowLayout. That way if the layout manager resizes the component only the panel will be resized, not the entire label.
If you need more help then post your SSCCE that demonstrates the problem so we can better understand your requirement.
Upvotes: 1
Reputation: 168825
Create a BorderedBufferedImage that accepts an int for borderThickness, a Color for the borderColor, and a BufferedImage. Create a new BufferedImage based on the new size (with size increased by 2 x borderThickness), draw the border, then draw the image inside.
Use the BorderedBufferedImage for the JLabel.
Upvotes: 1