-1

I want to thank you for the help so far and now I have come across another issue. I can make a background Panel just fine and placing Panels with Labels as well. I would like to stack either a Panel with Label over another Panel with Label. Or Stack Labels one on top of another. So it would be Background then JPanel with Label on top of the background and another JPanel with Label on top of the first JPanel with Label.

The start of my code that is working right now is below:

    // SWITCH 1
    switch1 = new JPanel();
    switch1.setLocation(24,348);
    switch1.setSize(55,83);
    switch1.setOpaque(false);
    background.add(switch1);

    sw1 = new JLabel();
    sw1.setIcon(SW1);
    sw1.setLocation(0,0);
    switch1.add(sw1);
    pack();

I would like to add either a smaller JPanel with JLabel on top of this one. Is that possible? I tried several ways and it does not work one way I tried was this:

    // SWITCH 2
    switch2 = new JPanel();
    switch2.setLocation(24,348); 
    switch2.setSize(45,73); 
    switch2.setOpaque(false);
    switch1.add(switch2);

    sw2 = new JLabel();
    sw2.setIcon(SW2);
    sw2.setLocation(0,0);
    switch2.add(sw2);
    pack();

That does NOT work. Thanks in advance for helping me solve this.

  • `setSize` ... `setLocation` ... do you understand how components get laid out in Swing? Maybe you should stop for a second and take a look at [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Sep 20 '18 at 22:17
  • `I want to thank you for the help so far` - well then don't forget to "accept" answers when you get help (see: https://stackoverflow.com/questions/52395907/jbutton-release-action) by clicking on the checkmark so everybody knows the problem has been solved. – camickr Sep 21 '18 at 00:25
  • It almost sounds like this would be better done using custom painting in a single `JPanel` (with no `JLabel` components, just drawing the images where and in whatever order needed). – Andrew Thompson Sep 21 '18 at 02:49

1 Answers1

1

Or Stack Labels one on top of another

Sounds to me like you want the OverlayLayout.

You set the layout manager of the panel to use an OverlayLayout. Then you add the labels to the panel.

The labels are painted in the reverse order that you add them to the panel.

Check out: Preventing Overlay Layout from shifting background image label for an example demonstrating how to use the OverlayLayout.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • you stated something I had not tried before and just did. "The labels are painted in the reverse order that you add them to the panel." Once I did that my original code worked! Thanks! – PilotinControl Sep 21 '18 at 04:53
  • `Once I did that my original code worked!` - well I'm still not sure you understand the concept of using layout managers. When you have components in a 2D layout there is no need to concern yourself with this issue. There is also no need to ever use the setSize() or setLocation() methods as this is the job of the layout manager. The order of components is only relevant when you use the OverlayLayout.because the components are layered on the Z-Axis. – camickr Sep 21 '18 at 14:22
  • I'm not using a layout manager as my layout setting is set to null: setLayout(null); – PilotinControl Sep 22 '18 at 15:06
  • Nowhere in the code you posted do you use a null layout. How do you expect us to provide a reasonable answer when the code you post is incomplete? In any case you should NOT be using a null layout. Swing was designed to be used with layout managers. Based on the simple example you posted there is definitely no reason to use a null layout. – camickr Sep 22 '18 at 16:19