3

I am using the netbeans builder "design" on a JFrame class. I have the traditional JFrame, I added into it (with the builder) a JPanel.

Now in the code, I want to add a background to this panel like this :

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
        addBackground();
    }

    private void addBackground()
    {
        Image bgImage;
        try {
            bgImage = ImageIO.read(new File("src/general/Mockup.png"));
            jPanel1.add(new NewJFrame.ImagePanel(bgImage));
            jPanel1.repaint();
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
}

I don't see anything, just an empty JFrame/JPanel.

Btw, I am using this for the ImagePanel code

@SuppressWarnings("serial")
class ImagePanel extends JPanel {
    private Image image;

    ImagePanel(Image image) {
        this.image = image;
    };

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
}
Kalzem
  • 7,320
  • 6
  • 54
  • 79
  • Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230). – nIcE cOw Feb 02 '13 at 01:55
  • 1
    Netbeans will tend to use the XML form file to build and display the form in the form designer, so, it's not likely to execute your addBackground method. Also, given the the fact that your ImagePanel doesn't haven't a default constrcutor, I'm surprised that the for. Designer would let you add it any way. Add a set/getImage method to the ImagePanel, this will let you use the form designer properties to set the image. I'm also concerned about the file path – MadProgrammer Feb 02 '13 at 07:22

3 Answers3

5
  • your class ImagePanel extends JPanel { must override getPreferredSize(),

  • because public void paintComponent(Graphics g) { (painting in Swing, Graphics(2D)) doesn't returns any size back to the container, simple returns Dimmension(0, 0)

  • JPanel has implemented FlowLayout (accepting only PreferredSize that came from its childs) in API, then your custom painting never will be visible on the screen

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

What is jPanel1? What layout does it use? How large is the ImagePanel object after the GUI has been rendered? Rather than go through all of these gymnastics, why not simply using a JLabel with the image (or an ImagePanel instance) as your JFrame's contentPane? Note that this type of coding is one reason I avoid using NetBeans generated code. Let me code it myself and I can fully control everything my GUI does and shows.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You are right, the Netbeans generated code is ... Well, I love Visual Studio Builder and XCode Builder. Last time I am doing Netbeans Builder – Kalzem Feb 02 '13 at 13:22
1

Why dont you simply add a JLabel and insert the image name in the constructor? Like:

JFrame background=new JFrame("back.png");

You can then add this label directly to the frame or to your JPanel.

dramaticlook
  • 653
  • 1
  • 12
  • 39
  • When I use JLabel with an setIcon, I get the right result, but I cannot put on top of the image anything like a JButton or JTextField. It automatically moves the JLabel to the side. They cannot overlay each other. – Kalzem Feb 01 '13 at 23:47
  • 3
    @BabyAzerty : Actually, one can do what you saying cann't be done. Have a look at these [wonderful answers](http://stackoverflow.com/q/10140800/1057230) related to this same thingy and this [example](http://stackoverflow.com/a/10245045/1057230). Here is one more related [example](http://stackoverflow.com/a/11428289/1057230), for how to add components to `JLabel`. – nIcE cOw Feb 02 '13 at 02:00