Hi! Could someone please give me any idea on how to add a background image to JInternalFrame without subclassing. Below I provide a code snippet, but the line jif.setBackgroundImage(backImage); is incorrect. Please, help me to fix this error. But I don't want to create a subclass, e.g. MyJInternalFrame. Thanks!

JInternalFrame jif = new JInternalFrame("Title", true, true, true, true) {
            private Image backgroundImage = null;
            public void setBackgroundImage(Image backgroundImage) {
                this.backgroundImage = backgroundImage;
            }

            protected void paintComponent(Graphics g) {
                if (isOpaque()) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                if (backgroundImage != null) {
                    g.drawImage(backgroundImage,0,0,this);
                }
            }
        };
        ImageIcon icon = new ImageIcon(SelectablePanel.class.getResource("/icons/bkg_palette.jpg"));
        Image backImage = icon.getImage();
        jif.setBackgroundImage(backImage); // THIS LINE PROVIDES A COMPILATION ERROR
...

The following is the compilation error: "cannot find symbol symbol: method setBackgroundImage(java.awt.Image)"

add a Panel to the frame and draw the background on that. You shouldn't really be drawing directly onto the Frame anyway. But, if you want to, I would say you will need to use the getContentPane() method.

>Could someone please give me any idea on how to add a background image to JInternalFrame without subclassing.
Not really. You'll need to create your own subclass if you want to be able to add a method like that. You can't add methods though anonymous declarations like you have above and then expect to call them on an instance of the parent class.

Is there even a setBackgroundImage() method? There is no sign of it in the API

Thanks! Then could you please give me some link, where I can find an example of constructor for JInternalFrame("Title", true, true, true, true)?

>Could someone please give me any idea on how to add a background image to JInternalFrame without subclassing.
Not really. You'll need to create your own subclass if you want to be able to add a method like that. You can't add methods though anonymous declarations like you have above and then expect to call them on an instance of the parent class.

>Is there even a setBackgroundImage() method?
No, there is not and that is the entirety of the issue. She is trying to add that method through an anonymous class definition without providing an actual named class definition for the subclass. It cannot be done like that. You can override existing methods with such a declaration, but you cannot add new public methods.

Oh, I assumed it was some method that I had overlooked or something.

What specific example of that constructor would you need? It's just setting the title and a few boolean variables. The API doc seems to explain it sufficiently.

Ok, I created a class MyJInternalFrame:

class MyJInternalFrame extends JInternalFrame {
        private String title;
        private boolean resizable;
        private boolean closable;
        private boolean maximizable;
        private boolean iconifiable;
        private Image backgroundImage = null;

        private MyJInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
            this.title = title;
            this.resizable = resizable;
            this.closable = closable;
            this.maximizable = maximizable;
            this.iconifiable = iconifiable;
        }
            
        public void setBackgroundImage(Image backgroundImage) {
            this.backgroundImage = backgroundImage;
        }

        @Override
        protected void paintComponent(Graphics g) {
            if (isOpaque()) {
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            if (backgroundImage != null) {
                g.drawImage(backgroundImage,0,0,this);
            }
        }
      }

And then:

MyJInternalFrame jif = new MyJInternalFrame("Title", true, true, true, true);
        ImageIcon icon = new ImageIcon(SelectablePanel.class.getResource("/icons/bkg_palette.jpg"));
        Image backImage = icon.getImage();
        jif.setBackgroundImage(backImage);

However, the background image does not appear (see attached file). What is wrong in my code? Thanks!

Nothing really stands out as wrong with that code. I don't think you really need the fillRect(background) section, but that's unrelated. How large is your icon image? Perhaps you need to specify the width and height in your drawImage call to make sure it's stretched to the frame size?

The image and JInternalFrame both have 336 x 525. So, I tried g.drawImage(backgroundImage,0,0,336,525,this), but the result is the same...:( If I just change the background color, then it works, but setting the background image fails.

I looked up JInternalFrame and I'd say akill10 was on the mark about using a panel to render the image. JInternalFrame does use a content pane to hold it's components, so that is what you would need to be drawing on and I don't think you can easily override that.

You should be able to just switch that class to extend JPanel instead and add that to your JInternal frame.

Ok, I extended JPanel and then:

JInternalFrame jif = new JInternalFrame("Beads Collection", true, true, true, true);
        ImageIcon icon = new ImageIcon(SelectablePanel.class.getResource("/icons/bkg_palette.jpg"));
        Image backImage = icon.getImage();
        MyJPanel pan = new MyJPanel();
        pan.setBackgroundImage(backImage);
        jif.getContentPane().add(pan);

As you may see from the attached file, there is a border around the image. How could I escape it?

I looked up JInternalFrame and I'd say akill10 was on the mark about using a panel to render the image. JInternalFrame does use a content pane to hold it's components, so that is what you would need to be drawing on and I don't think you can easily override that.

You should be able to just switch that class to extend JPanel instead and add that to your JInternal frame.

Have you tried g.drawImage(img, 0, 0, getWidth(), getHeight(), this); It may also be a layout issue. Try setting the contentpane layout to border layout and adding your panel at BorderLayout.CENTER.

Hmm...I changed the code as follows:

jif.getContentPane().setLayout(new BorderLayout());
        jif.getContentPane().add(pan,BorderLayout.CENTER);
...
g.drawImage(backgroundImage,0,0,getWidth(),getHeight(),this);

but still the border did not disappear. Any other idea? Well, it is not very critical, but from the design point of view I would like to somehow escape this border.

Have you tried g.drawImage(img, 0, 0, getWidth(), getHeight(), this); It may also be a layout issue. Try setting the contentpane layout to border layout and adding your panel at BorderLayout.CENTER.

I don't get such a border with my own test code here, so I'm not sure what else to suggest.

Ok, thank you very much. I will look through the code once again, perhaps I missed something.

I don't get such a border with my own test code here, so I'm not sure what else to suggest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.