Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:13:29+00:00 2026-06-12T07:13:29+00:00

I need to set a background image in my program. The structure of the

  • 0

I need to set a background image in my program. The structure of the main GUI is:

JFrame that contains – JPanel with BoxLayou that contains… etc

i need to put an image behind the first JPanel, but I don’t know how.

i wrote this code:

JPanel background = new JPanel();
JLabel labl = new JLabel("This is a dummy label this is a dummy label");
background.add(labl);
// TODO insert an image in background.
Component VERT_RA = Box.createRigidArea(new Dimension(0, 10));
Component HORI_RA = Box.createRigidArea(new Dimension(10, 0));
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
add(background);
add(main);
main.setOpaque(false);
main.add(VERT_RA);
JPanel a = new JPanel();
a.setLayout(new BoxLayout(a, BoxLayout.LINE_AXIS));
main.add(a);
main.add(VERT_RA);
a.add(HORI_RA);
JPanel services = new JPanel();
services.setLayout(new BoxLayout(services, BoxLayout.PAGE_AXIS));
a.add(services);
a.add(HORI_RA);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
a.add(right);
a.add(HORI_RA);     
JLabel lbl = new JLabel("SERVIZI");
lbl.setFont(new Font("SansSerif", Font.BOLD, 30));
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
lbl.setPreferredSize(new Dimension(100, 100));      
services.add(lbl);

but if I run it I can see only the “main” JPanel (the “SERVIZI” label).
I can only see background JPanel if I specify a setSize(x,y) method.

Is there any way to add a background image to my layout, without having to specify dimensions?

I also tried with setLayou(null) but i had to specify dimensions for all components by hand (not useful).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:13:30+00:00Added an answer on June 12, 2026 at 7:13 am

    You simply have to override the getPreferredSize() method for the respective JPanel, and make it return some valid Dimension Object. I guess this example might can help you in that direction :

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class PaintingExample
    {
        private CustomPanel contentPane;
        private JTextField userField;
        private JPasswordField passField;
        private JButton loginButton;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Painting Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            contentPane = new CustomPanel();        
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PaintingExample().displayGUI();
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private BufferedImage image;
    
        public CustomPanel()
        {
            setOpaque(true);
            setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
            try
            {
                /*
                 * Since Images are Application Resources,
                 * it's always best to access them in the
                 * form of a URL, instead of File, as you are doing.
                 * Uncomment this below line and watch this answer
                 * of mine, as to HOW TO ADD IMAGES TO THE PROJECT
                 * https://stackoverflow.com/a/9866659/1057230
                 * In order to access images with getClass().getResource(path)
                 * here your Directory structure has to be like this
                 *                 Project
                 *                    |
                 *         ------------------------
                 *         |                      |
                 *        bin                    src
                 *         |                      |
                 *     ---------             .java files             
                 *     |       |                   
                 *  package   image(folder)
                 *  ( or              |
                 *   .class        404error.jpg
                 *   files, if
                 *   no package
                 *   exists.)
                 */
                //image = ImageIO.read(
                //      getClass().getResource(
                //              "/image/404error.jpg"));
                image = ImageIO.read(new URL(
                            "http://gagandeepbali.uk.to/" + 
                                    "gaganisonline/images/404error.jpg"));
            }
            catch(IOException ioe)
            {
                System.out.println("Unable to fetch image.");
                ioe.printStackTrace();
            }
        }
    
        /*
         * Make this one customary habbit,
         * of overriding this method, when
         * you extends a JPanel/JComponent,
         * to define it's Preferred Size.
         * Now in this case we want it to be 
         * as big as the Image itself.
         */
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(image.getWidth(), image.getHeight()));
        }
    
        /*
         * This is where the actual Painting
         * Code for the JPanel/JComponent
         * goes. Here we will draw the image.
         * Here the first line super.paintComponent(...),
         * means we want the JPanel to be drawn the usual 
         * Java way first, then later on we will
         * add our image to it, by writing the other line,
         * g.drawImage(...).
         */
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }
    

    But if you don’t want to use inheritance, in your case, then you can simply add your image to the JLabel and then add your components to the JLabel by setting the Layout for the JLabel, as shown in this example and this example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Android Button: set onClick background image change with XML? I need that
i need to set image as background in android gridview. I have code which
I'm drawing an image using rgb pixel data. I need to set transparent background
I need to set a different background image dependant on the contents of a
I am new to windows mobile. I need to set a background image for
I set UINavigationBar background image at root view controller, but i need to remove
I need to set the fixed width for the background image. It should work
Possible Duplicate: Set size on background image with CSS? I need to find a
I'm coding a custom TableViewCell and I need to set a background image for
We need to set background image of a table. One way is to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.