Results 1 to 3 of 3
  1. #1
    weikang is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Help: How to auto-resize the image icon to fit in the jlabel

    Hi, anyone knows how to code to auto-resize the image icon to auto-fit into jLabel.

    Here is my code for my button:

    private JButton getJButtonSignUp() {
    if (jButtonSignUp == null) {
    jButtonSignUp = new JButton();
    jButtonSignUp.setBounds(new Rectangle(270, 497, 133, 50));
    jButtonSignUp.setIcon(new ImageIcon(getClass().getResource("/images/button.png")));
    jButtonSignUp.setFont(new Font("Arial", Font.PLAIN, 14));
    jButtonSignUp.setHorizontalTextPosition(SwingConst ants.CENTER);
    jButtonSignUp.setText("Sign Up");
    jButtonSignUp.setBorder(BorderFactory.createEmptyB order(2, 2, 2, 2));
    return jButtonSignUp;


  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    1. Your question asks about a JLabel (wrongly spelled jLabel) and your code relates to a JButton
    2. Don't double post the same question.
    3. A JLabel or JButton computes its preferred size according to is icon and/or text. If you need an image size that depends on the size of the component it is displayed in, custom painting may be a better option.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)


  3. #3
    weikang is offline Member
    Join Date
    Nov 2010
    Rep Power

Similar Threads

  1. How to resize images to fit JLabel?
    By Krooger in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 01:22 PM
  2. can't resize my JLabel
    By gib65 in forum AWT / Swing
    Replies: 10
    Last Post: 06-25-2010, 05:30 AM
  3. JTable Auto Resize Mode - not working as I expect
    By pahiker in forum AWT / Swing
    Replies: 5
    Last Post: 06-17-2010, 08:03 AM
  4. JScrollpane auto resize
    By knuth in forum New To Java
    Replies: 3
    Last Post: 09-29-2009, 10:12 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