Am trying to draw image on jpanel or jlabel when button is been click on, so i added actionperformed on my button as follows.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Image imag = new ImageIcon(this.getClass().getResource("img/top_bg.jpg")).getImage();
    showPix1.setLayout(new BorderLayout());
    showPix1.add(new paintPhotos(imag,20,20), BorderLayout.CENTER);
}

I have a separate class called paintPhotos that extends jLabel as follows

package myApp;
import java.awt.*;
import javax.swing.*;

public class paintPhotos extends javax.swing.JLabel {

public Image img; int w; int h;
public paintPhotos(Image img, int w, int h) {
this.img = img; this.w = w; this.h = h;
System.out.println("am paintclass");
}
@Override
public void paintComponent(Graphics p) {
System.out.println("am here");
super.paintComponent(p);
Graphics2D g2 = (Graphics2D) p;
p.drawImage(img, 0, 0, w, h, this);
}

}

Now when i click on the button it never draw the image, what am i doing wrong? and how can i draw the image on jLabel from my actionperformed event? pls any help will be highly appreciated. Thanks in advance

new ImageIcon is famous for not throwing an exception if it fails, sometimes not even returning a null, just a 0x0 image, so first thing is to check that you really have a valid image after line 3

But why are you doing all this anyway? - just set the JLabel's image icon and it will draw it for you

I think it draws it actual size, so you can create a scaled copy at whatever size you want and then just use the standard code?

... anyway, returning to your original question... if the image is OK then you probably just need to call repaint() for your JLabel - otherwize Swing has no way to know that it needs to be repainted.

i just made a try with this code and it work fine

final Image imag = new ImageIcon(this.getClass().getResource("img/top_bg.jpg")).getImage();
        Image newimg = imag.getScaledInstance(100, 100,      java.awt.Image.SCALE_SMOOTH);  
        showPix1.setIcon(new ImageIcon(newimg)); 

However, someone advice me not to use getScaledInstance so i will still like to see how to resovle my previous post. JamesCherrill recommend i just need to call repaint() for my JLabel, HOW? i try this.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Image imag = new ImageIcon(this.getClass().getResource("img/top_bg.jpg")).getImage();
showPix1.setLayout(new BorderLayout());
showPix1.add(new paintPhotos(imag,20,20), BorderLayout.CENTER);
showPix1.repaint();
}

i also try.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
showPix1.repaint();
}

But no of the two above work, so how do i just call repaint on my jLabel while the paintComponent is in another class that extends jLabel ???

You ideally need to call repaint on the paintPhotos instance, but on second thoughts, since you just added that component the redraw will be redundant (sorry). Maybe you need to call pack() on your container (showPix1?) to get the layout manager to size it properly, and since it has no contents that Swing knows about you'll probably need to set the size (preferredSize) of the paintPhotos object first

  1. this issue talking about remove then add a new JComponents to the (already) visible GUI, have to call revalidate(); and repaint(); for container where is placed ..., don't reacreate that on fly

  2. load all ImageIcons on the Apps startup, store that as local variable

  3. then you'll call only showPix1.setIcon(newimg);

This article has been dead for over six months. Start a new discussion instead.