Results 1 to 4 of 4
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Question How to resize images to fit JLabel?

    Hello, I need to load an image and use it in multiple places on my GUI but at different sizes.

    So far I am just loading it with
    Java Code:
    ImageIcon testIcon = new ImageIcon("images/testCard.jpg");
    Then I make a JLabel and size it to what ever size I need then use:
    Java Code:
    myJLabel.setIcon(testIcon);
    Then im left with a proper sized JLabel but only shows a portion of the image.
    How do I go about resizing this image to (in this case) shrink and resize to the size of the JLabel. I was looking through the ImageIcon java docs but didnt see anything about resizing.

    Thanks for any tips.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    > Then I make a JLabel and size it to what ever size I need then use:

    Thats the reverse of what you should be doing. You should be resizing the image and then let the label size itself based on the image.

    You can use image.getScaledImage(...) for the different size images.

  3. #3
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    Thank you, worked great!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. JScrollpane auto resize
    By knuth in forum New To Java
    Replies: 3
    Last Post: 09-29-2009, 10:12 PM
  2. how to resize an image
    By marodia in forum AWT / Swing
    Replies: 2
    Last Post: 08-21-2009, 04:10 AM
  3. Image Resize
    By jithan in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 09:26 AM
  4. resize tabs in jtabbedpane
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 03:02 AM
  5. Resize frame
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 11:18 PM

Posting Permissions

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