Using image or icon as background for JFrame instead of setBackground()

hello every one,
please, i'm trying to change the background of my FRAME using an icon or an image as the backgound whle still having my controls(JButton,JLabel,JTextField etc.) still on top of the image. Just like in HTML, for instace, the body tag can be use as follows:
<body background="c:\images\fly.gif">
and the image becomes the background.
Is this possible in java, if so please, how can i do this?
thank a lot for the help.

Check out this thread:
http://forum.java.sun.com/thread.jsp?forum=31&thread=353504

Similar Messages

  • How to set a   background for jFrame?

    Hai.i have a code for background image.i.e
    * TextOver.java
    * Created on June 23, 2008, 1:53 PM
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    public class TextOver
    private static final String IMAGE_PATH =
    "http://upload.wikimedia.org/wikipedia/commons/b/b5/HMS_Cardiff_%28D108%29_1.jpg";
    private BufferedImage image;
    private JTextArea textarea = new JTextArea(20, 40);
    private JPanel mainPanel = new JPanel()
    @Override
    protected void paintComponent(Graphics g)
    super.paintComponent(g);
    if (image != null)
    g.drawImage(image, 0, 0, this);
    public TextOver()
    URL imageUrl;
    try
    imageUrl = new URL(IMAGE_PATH);
    image = ImageIO.read(imageUrl);
    Dimension imageSize = new Dimension(image.getWidth(), image.getHeight());
    mainPanel.setPreferredSize(imageSize);
    JScrollPane scrollpane = new JScrollPane(textarea);
    textarea.setOpaque(false);
    scrollpane.setOpaque(false);
    scrollpane.getViewport().setOpaque(false);
    mainPanel.add(scrollpane);
    catch (MalformedURLException e)
    e.printStackTrace();
    catch (IOException e)
    e.printStackTrace();
    public JPanel getPanel()
    return mainPanel;
    private static void createAndShowGUI()
    admin_login_code a=new admin_login_code();
    a.setVisible(false);
    JFrame frame = new JFrame("TextAreaOverImage Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TextOver().getPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(false);
    public static void main(String[] args)
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    public void run()
    createAndShowGUI();
    i want to give this backgground to my existing jFrame something like
    import java.sql.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class admin_login_code extends javax.swing.JFrame {
    String admin_name;
    String password;
    Connection con;
    Statement stmt;
    ResultSet rs;
    public admin_login_code() {
    initComponents();
    jPasswordField1.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (!(Character.isDigit(c) ||
    (c == KeyEvent.VK_BACK_SPACE) ||
    (c == KeyEvent.VK_DELETE))) {
    getToolkit().beep();
    e.consume();
    jFormattedTextField1.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (!(Character.isLetter(c) ||
    (c == KeyEvent.VK_BACK_SPACE) ||
    (c == KeyEvent.VK_DELETE))) {
    getToolkit().beep();
    e.consume();
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jFormattedTextField1 = new javax.swing.JFormattedTextField();
    jLabel4 = new javax.swing.JLabel();
    jPasswordField1 = new javax.swing.JPasswordField();
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
    jLabel1.setForeground(new java.awt.Color(255, 0, 0));
    jLabel1.setText("ADMIN LOGIN");
    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
    jLabel2.setText("Admin Name");
    jFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jFormattedTextField1ActionPerformed(evt);
    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24));
    jLabel4.setText("Password");
    jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jPasswordField1ActionPerformed(evt);
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(39, 39, 39)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jLabel4)
    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(43, 43, 43)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
    .addContainerGap())
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(47, 47, 47)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(60, 60, 60)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jLabel4)
    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    jButton1.setText("Login");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    jButton3.setText("Exit");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton3ActionPerformed(evt);
    org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
    .add(38, 38, 38)
    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 110, Short.MAX_VALUE)
    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(55, 55, 55))
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel2Layout.createSequentialGroup()
    .add(38, 38, 38)
    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(30, Short.MAX_VALUE))
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(574, 574, 574)
    .add(jLabel1))
    .add(layout.createSequentialGroup()
    .add(459, 459, 459)
    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap(521, Short.MAX_VALUE))
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(149, 149, 149)
    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(43, 43, 43)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(42, 42, 42)
    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(250, Short.MAX_VALUE))
    pack();
    }// </editor-fold>
    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
    private void jFormattedTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Object src=evt.getSource();
    if(src==jButton3)
    dispose();
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Object src=evt.getSource();
    if(src==jButton1)
    admin_name=jFormattedTextField1.getText();
    password=jPasswordField1.getText();
    try
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcle","scott","root");
    stmt=con.createStatement();
    rs=stmt.executeQuery("select admin_name,password from admin_registration where admin_name='chandana' and password='8989' ");
    while(rs.next())
    if(admin_name.equals(rs.getString(1))&&password.equals(rs.getString(2)))
    admin_registration a=new admin_registration();
    a.setVisible(true);
    dispose();
    else
    JOptionPane.showMessageDialog(null,"Please enter admin name & password ");
    catch(ClassNotFoundException e)
    e.printStackTrace();
    catch(SQLException e)
    e.printStackTrace();
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    admin_login_code a=new admin_login_code();
    a.setVisible(true);
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    // End of variables declaration
    Can any one can help me how to set a background for jFrame?
    Thank you in advance.
    Edited by: forums.com on Jul 14, 2008 1:40 AM

    90% of the code you posted is not relevant to your question.
    If you want further help post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the problem.
    And don't forget to use code tags when posting code.

  • How can I use a pic from iPhoto as a background for a page, How can I use a pic as a background for a page

    How can I use a pic from iPhoto as a background for a page, How can I use a pic as a background for a page

    cd,
    Insert the picture and position it as you desire
    Make sure the picture is set to Floating in the Wrap Inspector
    Arrange > Send Objects to Background
    Regards,

  • How to use Image field in SQL-7 for curvs

    I sampling data from a NI-card. There will be a lot of data so I thought I should use Image-field in SQL-7 to save the hole curve. How do you do that?

    I would suggest looking into the Database Connectivity Toolset. It has complete SQL functionality and uses ADO to connect to many types of databases. I have it and it comes with example programs which demonstrate how to write to and read from databases using SQL.
    J.R. Allen

  • Background for JFrame

    hi
    I want to apply an image as a background to my application designed over the JFrame component. I want my other components like buttons and labels over that background. How is that possible?
    I am not using JApplet.
    I am new to Java & Swing so if anybody can send me some code ...
    Thanx

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class BackgroundImage extends JFrame
         JScrollPane scrollPane;
         ImageIcon icon;
         public BackgroundImage()
              icon = new ImageIcon("apache.jpg");
              JPanel panel = new JPanel()
                   public void paintComponent(Graphics g)
                        //  Approach 1: Dispaly image at at full size
                        g.drawImage(icon.getImage(), 0, 0, null);
                        //  Approach 2: Scale image to size of component
                        // Dimension d = getSize();
                        // g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
                        //  Approach 3: Fix the image position in the scroll pane
                        // Point p = scrollPane.getViewport().getViewPosition();
                        // g.drawImage(icon.getImage(), p.x, p.y, null);
                        setOpaque( false );
                        super.paintComponent(g);
              JButton button = new JButton( "Hello" );
              panel.add( button );
              scrollPane = new JScrollPane( panel );
              setContentPane( scrollPane );
         public static void main(String [] args)
              BackgroundImage frame = new BackgroundImage();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 300);
              frame.setVisible(true);
    }

  • Displaying image on canvas as background for joystick display

    i am displating my joystick on canvas sucessfully and now i want to add a background image to it for better display please guide in detail. thanks in advance .. ahsan

    Hello ahsan, you can use CanvasDrawBitmap to draw ain image on the canvas.
    When searching for a function on a particular control, you can right-click on the control and choose "Control help" in the context menu: in case of the canvas you'll arrive to this help page where some useful resources are grouped. You can look at Programming with Canvas Controls to get some tip on how to use the control in you applications, of simply choose Canvas Control Functions to get a list of all function that can be used on that control. In this page CanvasDrawBitmap is listed.
    Additionally, if you go to Help >> Find examples... and search for canvas, you'll get a list of sample projects related to that control: "Alphablend" is one of them that shows how to draw an image on the canvas.
    By using these tips you'll be able to proceed in your work without necessarily wait for some forum user to help you.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • While using a BDC in the background for some documents its hanging.

    Hi All,
    I had used BDC call transaction program and i am scheduling the program in the background. Normally it works fine but for some quotes it hangs and takes a long time. So its not going to other quote. Any solution how to kill the hanged quote and goto next one?

    Well, then you need to determine why (for which orders), the process is hanging. Compare with other orders, do a performance check, etc. There is no way for us to determine what the problem in this particular case is. We just don't have enough information to help you out here.

  • Can i use my own photos as Background for my Movie in iMovie for iPod touch (5g)? Like bluescreen/greenscreen effekt

    Example for my question:
    I make a Movie in the Garden but use a picture of
    a Big City as Background.
    Thats like a bluescreen Effekt
    Please help and i Hope that you can understand my question

    iMovie for iOS doesn't do green screen as far I can figure out.  However, if you want to shoot a green sceen video with a background on your iOS, here's one way of doing that.

  • How can I use an image as a folder background on icon

    vista updated firefox - how can I use an image as a folder background for an icon of that folder (for desktop)

    This is not a Firefox related issue or Firefox setting etc. This is a windows or OS setting, I provided the link below on how to change this in Windows Vista.
    See:
    '''[http://answers.microsoft.com/en-us/windows/forum/windows_vista-desktop/how-do-i-change-the-folder-icon-in-vista-to-one/01261170-4e02-4c4b-8c02-141d1524ad3e?auth=1]'''

  • Import an image as a canvas background

    I have been trying to import a gif image as a canvas background
    for a web forms application and am having a lot of trouble.
    Hopefully someone can help.
    I am running a SunOS 5.6 server,
    Windows NT client,
    Developer 6.0,
    Forms 6.0
    We have set up the FORMS60_OUTPUT and FORMS60_MAPPING
    variables. There is no trailing / in their defintion and TMPDIR
    is not set. I use the file -> import -> image command to import
    the image. It shows up in the forms builder window, but when
    deployed to the web it shows a broken image icon. The forms
    server is doing something, since a jpeg of my image gets written
    to the directory specified in FORMS60_OUTPUT, it just doesn't
    show up on the form.
    It is my understanding that the image is stored in the fmx . . .
    the online help says:
    "Graphic Objects: A graphic object is a static image (either
    vector or bitmap) that you import from the file system or
    database and use as a static graphical element in your
    application. Graphic objects are often used as background
    pictures. When you save and compile a form module, graphic
    objects are incorporated into the .FMB and .FMX files. You can
    manipulate graphic objects in the Layout Editor as you would
    other objects."
    But just in case I put a copy of the gif into the directory that
    the fmx is in, the directory that the html is in, and the
    directory that the button icons are getting pulled in from
    (these show up fine, by the way.) but none of this seems to work.
    I am at my a loss at this point and don't know what else to do.
    Please help me if you can.
    Thanks,
    -- Ben
    null

    Hi,
    Firefox and other browsers are just front ends that display email pages like any other web page. The actual emails, the settings, storage etc. are all stored and coming from the email provider's end and Firefox just formats and displays them nicely. You may have to go into the settings of the email account to see if the provider has an option to include images as signatures. If the provider hasn't setup an option to insert images as signatures, then it's may not be possible.

  • How to use images from ADFLib

    Hello OTN,
    My application is devided into several ADFLibs, one of them is CommonUI. It includes common skin and it is imported into every application part.
    There are some images which should be available in different parts, so I decided to put them in CommonUI.
    After deploying adflibCommonUI adn refreshing Resource Palette, somehow I expected to see this image there, but it isn't.
    Could someone, please, explain me, how to use images contained in imported ADFLib, for example, as imageLink icon?
    Thanks.
    ADF 11.1.2.1

    Hi,
    images need to be saved in a specific file structure in the JAR file to be accessible. See:
    http://www.oracle.com/technetwork/developer-tools/adf/learnmore/86-images-from-jar-427953.pdf
    Frank

  • Time Machine Background for my desktop or screen saver

    How can I use the vortex time machine background for my desktop or screen saver background?
    Thanks

    Hello,
    In short....you can't. You can use the static image as a wallpaper or slideshow ScreenSaver but you cannot use the "Vortex" engine for the animated Time Machine effects. It appears that the "Vortex" engine is part of the Resources for the Finder application itself as evidenced by the related files and images found in the Finder application Package. I don't know if anyone could create a "hack", utilizing the existing resources, to make a ScreenSaver file or not. I have zero experience with ScreenSavers. I just use them.
    I know that there are interesting integer parameters in some of the "Vortex" files but I do not have the "bullocks" to experiment with them.
    Sorry.....
    pw

  • When i use images in an applet, it shows the image url, how do i remove it?

    Ok, so when i open up an applet, and it uses images, it shows the url for each image, which is not good because my graphics could get stolen. Is there anyway to disable this?, i have never noticed it before.

    I notice the following from the documentation for one of the [ImageIcon constructors|http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.net.URL)]: "Creates an ImageIcon from the specified URL. The image will be preloaded by using MediaTracker to monitor the loaded state of the image. The icon's description is initialized to be a string representation of the URL."
    I guess if you are using this - or something similar - Java runtimes might decide to show the string representation of the URL while they are loading the image. In this particular case there is another form of the constructor which allows you to actually set the description string.

  • How can I use an image as background for the entire page?

    With CSS mill, the solid color was defined in the color.X.property file and there is no place to define an image to use as the background? Is it doable to use an image as the background for teh entire page? If so, where to add it and how to implement? Thank you!
    Hao Pan ([email protected])

    While I have been able to add the image to a JLabel and insert it into frames, so that it looks like a suitable background, I have found it impossible to place any other component over the top of said imageDid you set a layout manager for the label?
    Check out the [Background Panel|http://www.camick.com/java/blog.html?name=background-panel] for two different solutions depending on your exact requirement.

  • Image as a background for a JFrame

    Hi all,
    Is it possible to have an image as a background for a JFrame?
    How do you do it?
    plz help

    i personally inherit a panel...
      class ImagePanel extends JPanel {
          Image image;
          public ImagePanel(Image image) {
              this.image = image;
          public void paintComponent(Graphics g) {
              super.paintComponent(g); //paint background
              //Draw image at its natural size first.
              g.drawImage(image, 0, 0, this); //85x62 image
      } and call it with
      private Image icLogo =
    Toolkit.getDefaultToolkit().getImage(getClass().getResource("icLogo.gif"));
      private JPanel imgPanel = new ImagePanel(icLogo);Well, this should work with frames, too. In case it doesn't, you can simply add this panel to the frame.
    greets
    Jochen

Maybe you are looking for