4

Today while surfing through various questions, I encountered one QUESTION, this seems to me a bit weird, why would one wants to add a JPanel to a JLabel, are there any genuine reasons as to such situation can arise, so is it just trivial ?

Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143

3 Answers3

4

Yes there are genuine reasons to want to add components to a JLabel. Since it is trivially easy to set and swap ImageIcons on JLabels, it's not uncommon to want to use them as a backing image for your GUI.

Edit
You state:

Ahha mean to say, If I wanted my container to have a specific background, then I must be using JLabel as the platform, on which such things can reside ? Am I right ?

No, you don't absolutely have to use a JLabel for this as it is fairly easy to draw a background image in a JPanel if desired. Just draw the image in its paintComponent(...) method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Ahha mean to say, If I wanted my container to have a specific background, then I must be using `JLabel` as the platform, on which such things can reside ? Am I right ? – nIcE cOw Apr 13 '12 at 12:31
  • Wow, This looks interesting, no doubt as usual, informative answer :-) – nIcE cOw Apr 13 '12 at 12:52
  • 3
    *"Just draw the image in its paintComponent(...) method."* That's generally a good approach, but it shows a static view of animated images. See my answer for an animated image BG. :) – Andrew Thompson Apr 13 '12 at 13:10
4
this seems to me a bit weird, why would one wants to add 
a JPanel to a JLabel,

yes thats right

are there any genuine reasons as to such situation can arise, 
so is it just trivial?

no, isn't trivial, because only JFrame/JDialog/JWindow and JPanel have got pre_implemented LayoutManager, for rest of "Custom JComponent" you have to declare proper LayoutManager, programatically

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Wow, that's a very nice example +1 for this :-) – nIcE cOw Apr 13 '12 at 12:55
  • 2
    @nIcE cOw and I adding a salt to... JLabel has similair nature as a JViewport (my view), but JLabel blocking Mouse and Keyboard inputs, for example I'm not able to define (switchable) JViewport that consuming Mouse and Keyboard inputs, but this crazy JLabel missing repaint() in API for opaque(true), then on one side HFOE could be right, but Andrew opposed with Html and Icons, and I'm by default ignoring all good habbits (everything is possible) – mKorbel Apr 13 '12 at 13:04
  • *"tonight I might won't sleep, LOL"* This was copied over from a comment to a different answer since I thought I should 'fess up' that I heard this was possible only recently, from mKorbel. At that time I thought, "Huhh, neat! I'll have to chase that up later and see what I can shove in the label". :) – Andrew Thompson Apr 13 '12 at 13:26
  • @mKorbel What, with all this excitement going on? ;) – Andrew Thompson Apr 13 '12 at 20:27
  • @Andrew Thompson wrote "tonight I might won't sleep, LOL", I tried your idea about JLabel and animated gif, how easy that could be, but I missed VanGUI_ph's ideas ++++ 1k – mKorbel Apr 13 '12 at 21:58
4

An animated image as a BG for the GUI. I use HTML to resize this one (x3), but if it is already the desired size, you could set it directly as the Icon of the label.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class LabelAsBackground {

    public static final String HTML =
        "<html>" +
        "<style type'text/css'>" +
        "body, html { padding: 0px; margin: 0px; }" +
        "</style>" +
        "<body>" +
        "<img src='http://pscode.org/media/starzoom-thumb.gif'" +
        " width=320 height=240>" +
        "";

    LabelAsBackground() {
        JFrame f = new JFrame("Animated Image BG");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel contentPane = new JLabel(HTML);
        contentPane.setLayout(new GridLayout());
        JPanel gui = new JPanel(new GridLayout(3,3,15,15));
        gui.setOpaque(false);
        contentPane.add(gui);
        gui.setBorder(new EmptyBorder(20,20,20,20));
        for (int ii=1; ii<10; ii++) {
            gui.add( new JButton("" + ii));
        }
        f.setContentPane(contentPane);
        f.pack();
        //f.setResizable(false); // uncomment to see strange effect..
        f.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                LabelAsBackground lab = new LabelAsBackground();
            }
        });
    }
}

Not sure if it is 'genuine' or not. That seems a subjective term that requires much clarification. I've never used this method and only just figured it out, fumbling around tonight. ;)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • That's a promising start :-) +1 for this, but again the same thoughts, the whole thing can be achieved by doing it vice-versa, through normal steps. – nIcE cOw Apr 13 '12 at 13:02
  • *"doing it vice-versa, through normal steps"* How exactly? (I mean as in 'lines of code I can count'.) – Andrew Thompson Apr 13 '12 at 13:04
  • No no my bad, just got confused, in between. What I always believed, and what I saw today as a new approach. I never thought of `JLabel` being something, other than displaying the Image and the Text. This new behaviour, is completely unknown to me, seems like I need time to get to terms with this new thingy :-) MY EYES are WIDE OPEN , tonight I might won't sleep, LOL – nIcE cOw Apr 13 '12 at 13:15