0

I was trying to add an image I capture from a webcam to an existing JPanel when a button is clicked, but the JPanel never displays the image. Can anyone point me in the right direction please?

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

    try {
        grabber.start();
        IplImage img = grabber.grab();
        if (img != null) {

            img1 = img.getBufferedImage();
            ImageIcon icon = new ImageIcon(img1);
            label = new JLabel(icon);
            //photo is the name of the JPanel
            photo.add(label);
            photo.setVisible(true);
            grabber.stop();
            System.out.println("done");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

EDIT: This is the whole class. (Trimmed it to make it easier to read)

package honoursproject;

    import com.googlecode.javacv.OpenCVFrameGrabber;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


public class AddPerson extends javax.swing.JFrame {

/** Creates new form AddPerson */
public AddPerson() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    nameField = new javax.swing.JTextField();
    surnameField = new javax.swing.JTextField();
    photo = new javax.swing.JPanel();
    captureButton = new javax.swing.JButton();
    saveButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    photo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0,       0)));

    pack();
}// </editor-fold>

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    JFrame frame = new JFrame();
    try {
        grabber.start();
        IplImage img = grabber.grab();

        if (img != null) {

       img1 = img.getBufferedImage();
        ImageIcon icon = new ImageIcon(img1);
        label = new JLabel(icon);
        //photo is the name of the JPanel
        photo.add(label);
        photo.setVisible(true);
        grabber.stop();
        System.out.println("done");


            grabber.stop();
            System.out.println("done");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}                                             



public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new AddPerson().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JButton captureButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField nameField;
private javax.swing.JPanel photo;
private javax.swing.JButton saveButton;
private javax.swing.JTextField surnameField;
// End of variables declaration

}

davy307
  • 309
  • 3
  • 16
  • Any Exceptions? Have you already printed the icon etc. to see what their value is? – F. Müller Mar 08 '13 at 20:42
  • no exceptions, the program completes without errors and the "done" is printed out. – davy307 Mar 08 '13 at 20:46
  • Are you using a layout manager? Do you need to add more then one image at a time? – MadProgrammer Mar 08 '13 at 21:17
  • I am using a layout manager but no, i only need one image to be added at the click of a button. – davy307 Mar 08 '13 at 21:46
  • For better help sooner, post an [SSCCE](http://sscce.org/). To do that, factor out all the webcam complications and simply generate an image in code using `new BufferedImage(40,40,BufferedImage.TYPE_INT_RGB)`. See [this example](http://stackoverflow.com/a/13512826/418556) of an SSCCE that shows images. – Andrew Thompson Mar 09 '13 at 07:32

3 Answers3

3

If you're adding a new component to your photo JPanel you will need to update & paint the container:

photo.revalidate();
photo.repaint();

Alternatively, the JLabel could also be added at startup, allowing you to call setIcon to update the image.

Update:

You don't appear to add your JPanel photo to your JFrame anywhere:

add(photo);

Don't create another JFrame to display your image. Add it to your JPanel using the original JFrame. See this post.

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Did you call `revalidate`? – Reimeus Mar 08 '13 at 20:45
  • 1
    @davy307 Its possible that the image contains no data _or_ that the `JPanel` `photo` has no size. Consider posting an [SSCCE](http://sscce.org/) – Reimeus Mar 08 '13 at 21:15
  • @mKorbel, I have also tried the label.setIcon and it doesnt work, besides doesnt label = new JLabel(icon) do the same? – davy307 Mar 08 '13 at 21:38
  • @Reimeus, the image does contain data, this works perfectly when i use a JFrame, but because of the kind of GUI im trying to create i need the image in a JPanel which is embedded in a JFrame. – davy307 Mar 08 '13 at 21:39
  • Sounds like an issue with the layout manager, can't help without seeing the frame code. – Reimeus Mar 08 '13 at 21:41
  • where can i post the code for it so that you can see it? Im new to asking a question ^^ – davy307 Mar 08 '13 at 21:46
  • If you don't have _too much_ code, you can click on the _edit_ link and insert more code. – Reimeus Mar 08 '13 at 21:47
  • Ok, I added pretty much the whole of the class, hope it helps. – davy307 Mar 08 '13 at 21:53
  • Sorry, that JFrame that you see created was just used to test that the image was actually there, I don't actually use that at all. I have already tried an InternalJFrame and it still doesnt display anything. However, the pooint of this is not to use a Frame but a Panel, as my goal is to make it look like the photo from an ID. – davy307 Mar 08 '13 at 22:22
  • don't to recreate the JComponents on runtime, change only its value (Icon) – mKorbel Mar 09 '13 at 06:30
0

I solved it by using a JLabel instead of a JFrame or a JPanel and it works.

davy307
  • 309
  • 3
  • 16
0

You forgot to call setIcon:

label.setIcon(icon);
user247702
  • 23,641
  • 15
  • 110
  • 157