0

I know this question has been asked many times, but I can't get any of the answers to work for me. here is the code produced by netbeans for my image:

jLabel2 = new javax.swing.JLabel();
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pathtomastery/pathToMastery.png"))); // NOI18N
progressPanel.add(jLabel2);
jLabel2.setBounds(130, 20, 460, 80);

What can I add to this code to make my image resize to fit the size of the label?

  • Try my solution and let me know whether it works. – user3437460 Dec 06 '15 at 20:28
  • `but I can't get any of the answers to work for me` - well post links to the answers you tested. Why would they work for other people but not you. Post a proper [SSCCE](http://sscce.org/) when you ask a question. – camickr Dec 06 '15 at 23:10

3 Answers3

0

Have you tried:

jLabel2.setPreferredSize(new Dimension(460, 80));
user3437460
  • 17,253
  • 15
  • 58
  • 106
0

Set the label with a new icon and use BufferedImage like:

        BufferedImage bi = new BufferedImage(
            100, 100, BufferedImage.TYPE_INT_ARGB);
        bi.getGraphics().drawImage(image, 0, 0, null);
        label.setIcon(new ImageIcon(bi));
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

You can check out Darryl's Stretch Icon. The image will automatically be scaled to fill the space available to the label.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Finally got this working! :) Thanks. This is the only method I managed to work with. It is very simple which it needs to be as I am a beginner. – Ayman Harake Dec 19 '15 at 20:09