3

I have the following code:

try {
        File file_background = new File(
                "C:\\Users\\xxxx\\Desktop\\background.png");
        ImageIcon icon_background = new ImageIcon(
                ImageIO.read(file_background));
        JLabel background = new JLabel(icon_background);
        window.setContentPane(background);

        File file_car = new File(
                "C:\\Users\\xxxxxx\\Desktop\\car.png");
        ImageIcon icon_car = new ImageIcon(ImageIO.read(file_car));

        JLabel car = new JLabel(icon_car);
        car.setVisible(true);

        background.add(car);
        // TODO Get car showing on top of the background label

    } catch (IOException e) {

        e.printStackTrace();
    }

Where I'm attempting to have the car label show on TOP of the background label. But I'm only getting the background JLabel showing. I'm new to SWING so any suggestions to what steps I'm missing would be great.

Force444
  • 3,321
  • 9
  • 39
  • 77

3 Answers3

4

..I want to move it a later stage. But right now I want it to show first :)

There are two ways,

1st.

  • Put JLabel car to JPanel, drawing an Image by using paintComponent, instead of JLabel background (advantage JPanel is container with proper notifications for LayoutManager).

  • Put JLabel car to JLabel background, but JLabel haven't implemented any LayoutManager, have to set desired.

    1. Advantage all images in JLabel are static, with zero CPU and GPU inpact ad consumption in compare with paintComponent.
    2. Disadvantage JLabel isn't container and with proper notifications for LayoutManager, required a few code lones moreover in compare with JLabel placed in JPanel, for movement (AbsoluteLayout) is quite good solution.

2nd.

Draw both Images by using BufferedImage and Graphics.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

Add them both to a JPanel that uses OverlayLayout. It is not ok to add a JLabel to another JLabel.

This code has not gone through a compiler so take it for what it is :)

JPanel panel = new JPanel(new OverlayLayout());
panel.add(background);
panel.add(car);
Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
  • Can you please explain why it's not okay to add a JLabel to another JLabel? Thanks – Force444 Nov 20 '12 at 14:21
  • Look at the documentation for JPanel. It is designed to contain other JComponents (and JLabel is a JComponent). JLabel indirectly inherits from Container which is an AWT component designed to contain other AWT components. It is confusing, but I would guess that you will run into several issues similar to what you just did (layout not working) if you don't use a JPanel. – Tobias Ritzau Nov 20 '12 at 14:26
  • Oh okay. So you want me to change them both from JLabel to JPanel and then use the OverlayLayout in order to display one over the other? – Force444 Nov 20 '12 at 14:29
  • +1 for layout JLabel by OverLayLayout, but this is one good reason to lay JLabel in JLabel, – mKorbel Nov 20 '12 at 14:48
  • @mKorbel, if it is a good idea to put a JComponent in another JComponent, then why is there a JPanel? – Tobias Ritzau Nov 20 '12 at 14:55
  • correct question, you are right, but depends of, for this issue and layed by OverLayLayout doesn't matter, because is about null layout, but for rest of standard layout managers [could be non_sence do that or not ???](http://stackoverflow.com/questions/8575641/how-returns-xxxsize-from-jcomponents-added-to-the-jlabel) still opened question – mKorbel Nov 20 '12 at 15:00
  • there are a few question last year (about icon into icon in JLabel), where is used JLabel as contianer .... – mKorbel Nov 20 '12 at 15:02
  • Thanks, really interesting :) – Tobias Ritzau Nov 20 '12 at 15:27
  • 1
    There's an `OverlayLayout` example [here](http://stackoverflow.com/a/13437388/230513). – trashgod Nov 20 '12 at 19:13
0

Works.. Added the following to make it display:

car.setBounds(200, 200, 200, 200);

Apparently it's because by default a null layout manager is used. So setting the bounds of the label will enable it to display since the default size is 0.

Force444
  • 3,321
  • 9
  • 39
  • 77
  • 1
    the default layout manager is never the null layout manager...you must be setting this explicitly somewhere, which I highly recommend you change. – mre Nov 20 '12 at 14:42