Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3393778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:05:29+00:00 2026-05-18T04:05:29+00:00

I have problem with creating border for an icon in JLabel. I have JPanel

  • 0

alt text

I have problem with creating border for an icon in JLabel. I have JPanel in which I set it into GridLayout. I added Jlabel on the JPanel. The size of the JLabel were according to the size of the icon. However when I tried to set the border on the icon, it created border according to the size of the grid and not the size of the icon inside the grid.

How can I create a border around the image not on the size of the grid?

Why the border followed the size of the grid and not the size of the imageIcon?

JPanel panel= new JPanel(new GridLayout(ROWS,COLS,2,2)); 
panel.setsize(600,600);

....
JLabel = new JLabel(icon, JLabel.LEFT);
label.setVerticalAlignment(SwingConstants.TOP);
...
label.setborder(BorderFactory.createLineBorder(Color.RED,5));
panel.add(label);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:05:30+00:00Added an answer on May 18, 2026 at 4:05 am

    I solved the problem. Thanks to this site http://forums.oracle.com/forums/thread.jspa?messageID=5785467&#5785467

    Image image = icon.getImage().getScaledInstance(widthX,heightY, image.SCALE_SMOOTH);            
    icon.setImage(image);   
    
    int borderWidth = 1;
    int spaceAroundIcon = 0;
    Color borderColor = Color.BLUE;
    
    BufferedImage bi = new BufferedImage(icon.getIconWidth() + (2 * borderWidth + 2 * spaceAroundIcon),icon.getIconHeight() + (2 * borderWidth + 2 * spaceAroundIcon), BufferedImage.TYPE_INT_ARGB);
    
    Graphics2D g = bi.createGraphics();
    g.setColor(borderColor);
    g.drawImage(icon.getImage(), borderWidth + spaceAroundIcon, borderWidth + spaceAroundIcon, null);
    
    BasicStroke stroke = new BasicStroke(5); //5 pixels wide (thickness of the border)
    g.setStroke(stroke);
    
    g.drawRect(0, 0, bi.getWidth() - 1, bi.getHeight() - 1);
    g.dispose();
    
    label = new JLabel(new ImageIcon(bi), JLabel.LEFT);
    label.setVerticalAlignment(SwingConstants.TOP);      
    
    panel.add(label);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a calendar using a HTML table. I have a problem where
I have a problem creating ND arrays dynamically. So for example: int **A =
I am creating a Silverlight dialog class which is more MVVM-friendly. The ChildWindow class
I have 2 points for today I. I have a controller in which i
I have a mx.components.List component with a bunch of custom styles: <mx:Style> .dropDownListStyle {
I am currently working on a project which allows users to design a small
I have a query string that is dynamic. The items I am searching for
I have used update panel and in that update panel I have kept ASP
I want to integrate the iTunes library into my application so the user can
As seen on Introduction to Algorithms ( http://mitpress.mit.edu/algorithms ), the exercise states the following:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.