Custom Component undefined constructor in Java

Tags: ,



I’m trying to replace my Jlabels with a custom JComponent called PhotoComponent. So far, I’ve followed all the steps laid out from my instructors video on how to create the component. However, trying to replace the desired Jlabels with these two errors:

    The constructor PhotoComponent() is undefined
    The method currentPic(ImageIcon) is undefined for the type new ActionListener(){}

This is the code for my custom component:

import javax.swing.ImageIcon;
import javax.swing.JComponent;

public class PhotoComponent extends JComponent {
    private ImageIcon pic;

    public PhotoComponent(){

    }

    public PhotoComponent(ImageIcon p){
        pic=p;
        this.setSize(pic.getIconWidth(),pic.getIconHeight());
    }

    public void PaintComponent(Graphics g){
        pic.paintIcon(this, g, 0, 0);
    }
}

Answer

The method currentPic doesn’t exist, you’ve defined currentPic as an instance of PhotoComponent, so I’m assuming you want to call a method on it.

If you want to create a new instance of PhotoComponent, then you should be using

currentPic = new PhotoComponent(picList.get(pos))

But personally, I wouldn’t be doing this, instead I’d have a setIcon method defined in PhotoComponent so I could simply change the image it displays

public class PhotoComponent extends JComponent {

    private ImageIcon pic;

    public PhotoComponent() {

    }

    public PhotoComponent(ImageIcon p) {
        pic = p;
        this.setSize(pic.getIconWidth(), pic.getIconHeight());
    }
    
    public void setIcon(ImageIcon icon) {
        pic = icon;
        repaint();
    }

    public void PaintComponent(Graphics g) {
        pic.paintIcon(this, g, 0, 0);
    }
}

Then, you’d change all the calls like currentPic(picList.get(pos)); to currentPic.setIcon(picList.get(pos));

How ever, PhotoComponent is not really set up properly, as it won’t actually paint anything

First, setSize should be avoided, as many components will simply ignore it and use the sizing hints, preferred/minimum/maximum size, instead.

Another issues is PaintComponent is defined incorrectly. Remember, Java is case sensitive, this means if you want to override a method, you need to spell it correctly.

You should replace it with something more like…

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    pic.paintIcon(this, g, 0, 0);
}

So, taking the above to comments into account, PhotoComponent should look something more like…

public class PhotoComponent extends JComponent {

    private ImageIcon pic;

    public PhotoComponent() {

    }

    public PhotoComponent(ImageIcon p) {
        pic = p;
    }

    public void setIcon(ImageIcon icon) {
        pic = icon;
        revalidate()
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        if (pic == null) {
            return new Dimension(0, 0);
        }
        return new Dimension(pic.getIconWidth(), pic.getIconHeight());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        pic.paintIcon(this, g, 0, 0);
    }
}

Also, done use mainPic.add(currentPic);, this isn’t how you should be using a JScrollPane … and in fact, you only need to call mainPic.setViewportView(currentPic); once, when you initialise the UI

I would recommend that you read and book mark



Source: stackoverflow