5

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

Excalibur
  • 467
  • 2
  • 7
  • 15
  • 1
    Yes it can be done, but it's kind of hard to help you with you just saying what you want and not telling us what you've tried or where exactly you're stuck. Please help us help you by improving this question, including showing your code attempt, preferably an [sscce](http://sscce.org). For more on what will help us, please look at Jon Skeet's blog: [Asking the Perfect Question](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) – Hovercraft Full Of Eels Sep 03 '12 at 21:09
  • What is your question? Even if it seems obvious to you, it pays to add an explicit question. – Andrew Thompson Sep 04 '12 at 02:05
  • And [this](http://msmvps.com/blogs/jon_skeet/archive/2012/11/24/stack-overflow-question-checklist.aspx) is the new short list to write a good question from Jon Skeet's blog. – Adil Apr 19 '13 at 10:45

3 Answers3

10

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Yeah, I kind hinted at that at the end,it just seems, messy to me, not to say it can't be done, but I've not context for the question :P – MadProgrammer Sep 03 '12 at 21:15
  • Thanks, I think this is gonna be the best method, can you provide me with a code example plz? – Excalibur Sep 04 '12 at 09:37
  • Gotta love cheap jokes by programmers :) thanks a lot for this, it's for a quiz system I'm working on. – Excalibur Sep 04 '12 at 17:24
0

At runtime:

  • remove the label from its parent
  • add a container, which supports layers
  • add the 2x layer, but keep the Z order

Enjoy. (No full code given for copy-paste)

-3

you can do that by this:

JLabel l1=new JLabel();
JLabel l2=new JLabel();
l1.add(l2, JLabel.NORTH);
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97