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

  • SEARCH
  • Home
  • 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 8475867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:00:52+00:00 2026-06-10T18:00:52+00:00

I am pretty sure that this question has been asked before, but my case

  • 0

I am pretty sure that this question has been asked before, but my case is slightly different as in i am trying to place a JLabel on top of a JLabel acting as a background, I want to display changing numbers using the JLabels and the numbers need to display over the background, however i am a bit of a swing n00b, thanks in advance, Jonathan

  • 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-10T18:00:54+00:00Added an answer on June 10, 2026 at 6:00 pm

    Without fully appreciating your requirements, if you simply need to display text over a background image, you’d be better off placing the label on top a custom panel which is capable of painting your background.

    You get the benefit of a layout manager without the mess.

    I’d start by having a read trough Performing Custom Painting and Graphics2D Trail.

    If that seems to daunting, JLabel is actually a type of Container, meaning it can actually ‘contain’ other components.

    EXAMPLE

    Background pane…

    public class PaintPane extends JPanel {
    
        private Image background;
    
        public PaintPane(Image image) {     
            // This is just an example, I'd prefer to use setters/getters
            // and would also need to provide alignment options ;)
            background = image;            
        }
    
        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));            
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            if (background != null) {                
                Insets insets = getInsets();
    
                int width = getWidth() - 1 - (insets.left + insets.right);
                int height = getHeight() - 1 - (insets.top + insets.bottom);
    
                int x = (width - background.getWidth(this)) / 2;
                int y = (height - background.getHeight(this)) / 2;
    
                g.drawImage(background, x, y, this);                
            }
    
        }
    
    }
    

    Constructed with…

    public TestLayoutOverlay() throws IOException { // Extends JFrame...
    
        setTitle("test");
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        PaintPane pane = new PaintPane(ImageIO.read(new File("fire.jpg")));
        pane.setLayout(new BorderLayout());
        add(pane);
    
        JLabel label = new JLabel("I'm on fire");
        label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
        label.setForeground(Color.WHITE);
        label.setHorizontalAlignment(JLabel.CENTER);
        pane.add(label);
    
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    
    }
    

    And just to show that I’m not bias ;), an example using labels…

    public TestLayoutOverlay() {
    
        setTitle("test");
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        JLabel background = new JLabel(new ImageIcon("fire.jpg"));
        background.setLayout(new BorderLayout());
        add(background);
    
        JLabel label = new JLabel("I'm on fire");
        label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
        label.setForeground(Color.WHITE);
        label.setHorizontalAlignment(JLabel.CENTER);
        background.add(label);
    
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    
    }
    

    On fire

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

Sidebar

Related Questions

i'm pretty sure that this question has allready been asked (but i didn't find
sorry if this question has been asked before, I searched but wasn't sure on
This question has been asked before but, I'm a newbe and having issues trying
I am sure this question has been asked but I can't find anything that
I'm pretty sure it hasn't, but apologies if this question has already been asked.
I'm sorry if this question has been asked before, but I'm not even sure
Pretty sure this has been asked already, but I don't know what to search
I'm sure this question has been answered before, but I can't find an answer
I'm pretty sure that this question already has been answered somewhere. It just seems
I suspect this has been asked before, but can't seem to find a question

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.