0

In my interface I have one JPanel with null layout as a background label holder, with no other components. I'm trying to add a row of images on top of that background as a GridLayout. I have made sure that the Icons work properly when added to a JLabel but when I try to add the Grid Panel to the background panel, it doesn't show the images. This is my code so far:

jPanel1 = new javax.swing.JPanel();
backgroundLabel = new javax.swing.JLabel();
backgroundLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ile/Background.jpg")));

jPanel1.add(backgroundLabel);

That is the background, then I try to add the Grid of images on top of that:

JPanel gridPanel = new JPanel(new GridLayout(9,3));
JLabel ashes = new JLabel();
ImageIcon ashesIcon = new ImageIcon(getClass().getClassLoader().getResource("Resources/Ashes.png"));
ashes.setIcon(ashesIcon);
gridPanel.add(ashes);
jPanel1.add(gridPanel);
jPanel2.setVisible(true);

But nothing is being displayed at all, any advice?

Eckersley
  • 79
  • 9
  • 1
    Dont use null layout, use proper layout – Blasanka Aug 10 '17 at 12:10
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 10 '17 at 12:12
  • 2
    .. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 10 '17 at 12:13

1 Answers1

0

I have one JPanel with null layout as a background label holder,

Not based on the code you posted. By default a JPanel uses a FlowLayout.

I'm trying to add a row of images on top of that background as a GridLayout.

If you want components to be displayed on top of an image then there are three solutions I can think of:

  1. paint the image on the JPanel and then just set the layout manger and add your other components

  2. Add the background label to the panel, then set the layout manager of the label and add the other components to the label (not the panel)

  3. Use the OverlayLayout. This layout will allow you to add multiple components to the same panel and then components will be stacked on top of one another in the Z dimension. You will need to make the panel containing the GridLayout non-opaque.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I made the background use OverLayLayout and then set the Grid Layout on top of it with the images and made sure opaque is set off. Still none of the images are visible. Pathing is correct and I just can't understand why it doesn't work. – Eckersley Aug 11 '17 at 12:13
  • @e, `I made the background use OverLayLayout` - no that is not what was suggested or how the OverlayLayout works. You make a normal panel use the OverlayLayout. Then you add the panel with the images to this panel and then you add the background panel to this panel. So 3 components are involved. – camickr Aug 11 '17 at 14:11