3 Replies Latest reply: Oct 7, 2004 6:00 AM by 807587 RSS

    how to set image as background

    807587
      Hello

      I need set an image file (.JPG) as the background in the panel which will put them in the dialog window, and then it will allow me to do other GUI paint later, such as add dropdown list , label in this imaged panel. I don't know is it possible to do it? if so how to do it ?

      Thanks for the help!


        • 1. Re: how to set image as background
          807587
          http://forum.java.sun.com/thread.jsp?forum=57&thread=506054
          • 2. Re: how to set image as background
            807587
            Thanks for the reply and great sample code.

            I try to do some test to use your sample, but it gave me the error that could not find the method setBorder() in class Container ? I can't find out the reason. if I test to use your scrollpane sample, I can load and see the image backgroud looks very good.
            ---------------------------
            Runnable r = new Runnable() {           
            public void run() {               
            cp.setBorder(bkgrnd);
            cp.repaint();
            }
            };
            --------------------------

            Another questions is that how can I implement this as a regular panel in my JBuider design tool so I can add some other components thing on top of this imaged panel?

            Thanks for your help!


            • 3. Re: how to set image as background
              807587
              > I try to do some test to use your sample, but it gave me the error that could not find the method setBorder() in class Container ?

              Woops! I must have dashed off that second code posting. Container doesn't have a border property,
              and the content pane property is of type Container. However, the default content pane is a JPanel
              (using a BorderLayout) and JPanel, like all subclasses of JComponent, inherits its border propertty.
              So, you can either cast cp to JPanel or do the following:
              import java.awt.*;
              import java.io.*;
              import java.net.URL;
              import javax.imageio.*;
              import javax.swing.*;
              import javax.swing.border.*;
              
              public class BackgroundBorderExample {
                  public static void main(String[] args) throws IOException {
                      JFrame.setDefaultLookAndFeelDecorated(true);
              
                      final JPanel cp = new JPanel(new BorderLayout());
                      JFrame f = new JFrame("BackgroundBorderExample");
                      f.setContentPane(cp);
                      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      f.setSize(600,400);
                      f.setLocationRelativeTo(null);
                      f.setVisible(true);
              
                      String url = "http://today.java.net/jag/bio/JagHeadshot.jpg";
                      final Border bkgrnd = new CentredBackgroundBorder(ImageIO.read(new URL(url)));
                      Runnable r = new Runnable() {
                          public void run() {
                              cp.setBorder(bkgrnd);
                              cp.repaint();
                          }
                      };
                      SwingUtilities.invokeLater(r);
                  }
              }
              Another questions is that how can I implement this as a regular panel in my JBuider design tool so I can add
              some other components thing on top of this imaged panel?
              I'll leave that question for posters who know JBuilder. I use IDEs but I wouldn't touch a "design tool"
              with a 10 foot JPole.