4

I want to add a background image to my JFrame but when I do it using the code below, I'm unable to add other elements like JLabel or JTextField.

ImageIcon icon = new ImageIcon("src/images/back.jpg");
backImage = icon.getImage();
BackgroundImagePanel contentPane = new BackgroundImagePanel();
contentPane.setBackgroundImage(backImage);
this.setContentPane(contentPane);

Can you tell me please if there is another way to add JTabbedPane to a JFrame with a background ?

Thank you.

bnabilos
  • 2,234
  • 4
  • 23
  • 30
  • 1
    Possible duplicate of..... [http://stackoverflow.com/questions/2141020/jpanel-with-background-image-with-other-panels-overlayed](http://stackoverflow.com/questions/2141020/jpanel-with-background-image-with-other-panels-overlayed) [http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe](http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe) [http://stackoverflow.com/questions/2645452/background-image-in-a-jframe](http://stackoverflow.com/questions/2645452/background-image-in-a-jframe) [http://stackoverflow.com/questions/2706054/background-image-i – Tansir1 Jun 02 '10 at 18:23

2 Answers2

4

Like this?

Addendum: "Normally you would invoke super.paintComponent(g) first, but since the image will cover the entire background there is no need to do this."—camickr

Addendum: See also the opacity property.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Imager {

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

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ImagePanel("image.jpg"));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private static class ImagePanel extends JPanel {

        BufferedImage img;

        ImagePanel(String name) {
            this.setToolTipText(name);
            this.add(new JLabel(name));
            try {
                img = ImageIO.read(new File(name));
                this.setPreferredSize(new Dimension(
                    img.getWidth(), img.getHeight()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            // super.paintComponent(g);
            g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
        }
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • nitpicking Monday again :-) a) super(true) is redundant, panel is double-buffering by default b) even when covering everything, it might have transparent areas - so not calling super is complying to super's contract only if opaque == false. which it isn't by default and can be changed by client code at any time. So it's not really safe, and letting your IDE type one additional line is ... :-) – kleopatra Dec 05 '11 at 10:43
2

Try adding this:

//4. Size the frame.
this.pack();

//5. Show it.
this.setVisible(true);

Taken from How to Make Frames.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
npinti
  • 51,780
  • 5
  • 72
  • 96