Results 1 to 7 of 7
  1. #1
    Rothschild is offline Member
    Join Date
    Jan 2015
    Posts
    1
    Rep Power
    0

    Red face How to resize the icon of the button?

    Hello folks,

    so I have got a button and there is this property assigned to it:

    button.setIcon(new ImageIcon("src\\image.jpg"));

    The icon is not displayed in full, as it is larger than the button. What property can I use to resize the icon? I have tried few and it did not work. Any help appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to resize the icon of the button?

    Quote Originally Posted by Rothschild View Post
    Hello folks,

    so I have got a button and there is this property assigned to it:

    button.setIcon(new ImageIcon("src\\image.jpg"));

    The icon is not displayed in full, as it is larger than the button. What property can I use to resize the icon? I have tried few and it did not work. Any help appreciated.
    Nothing a small wrapper class can't solve:

    Java Code:
    public class ScaledIcon implements Icon {
    
    		private int width, height;
    		private Image image;
    		
    		public ScaledIcon(int width, int height, Image image) {
    			this.width= width;
    			this.height= height;
    			this.image= image;
    		}
    		
    		public void paintIcon(Component c, Graphics g, int x, int y) {
    			g.drawImage(image, x, y, width, height, c);
    		}
    
    		public int getIconWidth() { return width; }
    
    		public int getIconHeight() { return height; }
    		
    	}
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to resize the icon of the button?

    Quote Originally Posted by Rothschild View Post
    What property can I use to resize the icon? I have tried few and it did not work.
    The right thing would be to post what exactly you tried.

    Quote Originally Posted by JosAH View Post
    Nothing a small wrapper class can't solve
    That could mess up the aspect ratio though.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to resize the icon of the button?

    Quote Originally Posted by DarrylBurke View Post
    That could mess up the aspect ratio though.
    I know, but you either control the overall size (including the aspect ratio) or you control the height and width ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to resize the icon of the button?

    Or allow one to chose which dimension to favor when scaling.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to resize the icon of the button?

    Quote Originally Posted by jim829 View Post
    Or allow one to chose which dimension to favor when scaling.
    Sure, feel free to add it to that little class; it's in the public domain anyway ;-)

    kind regards,

    Jos ( <--- benevolent on Fridays )
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to resize the icon of the button?

    Quote Originally Posted by jim829 View Post
    Or allow one to chose which dimension to favor when scaling.
    Or scale it to fit the component:
    https://tips4java.wordpress.com/2012.../stretch-icon/
    https://tips4java.wordpress.com/2012/04/08/shrink-icon/

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  2. Adding another icon to an icon
    By whateverme in forum New To Java
    Replies: 11
    Last Post: 03-29-2011, 08:00 PM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  4. Replies: 2
    Last Post: 11-24-2010, 02:38 PM
  5. Window resize icon...
    By pele in forum SWT / JFace
    Replies: 3
    Last Post: 06-09-2008, 08:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •