I have a panel with the code below
public class PhotoBox extends JPanel {
private JLabel photoIcon;
private JLabel photoName;
private JLabel print;
private ImageHelper imageHelper;
public PhotoBox(File file, JLabel print) throws IOException {
imageHelper = new ImageHelper();
this.photoIcon = new JLabel();
this.photoIcon.setIcon(imageHelper.createThumbnails(file));
this.photoIcon.setMaximumSize(new Dimension(200, 150));
this.photoIcon.setAlignmentX(Component.CENTER_ALIGNMENT);
this.photoName = new JLabel();
photoName.setText((file.getName().length()) > 20 ? file.getName().substring(0,10)+".." : file.getName());
this.photoName.setAlignmentX(Component.CENTER_ALIGNMENT);
this.print = new JLabel();
this.print.setText(print.getText());
this.print.setAlignmentX(Component.CENTER_ALIGNMENT);
setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
setBorder(BorderFactory.createLineBorder(Color.black));
add(this.photoIcon);
add(this.photoName);
add(this.print);
}
}
Now I am adding a list of such a panel to another JPanel with a GridLayout
JPanel tile = new JPanel();
GridLayout layout = new GridLayout(0,4);
tile.setLayout(layout);
PhotoBox vBox = new PhotoBox(file,filePrints.get(file.getName()));
tile.add(vBox);
But I finally figured out something like this

I don’t understand why this entire space occupies all my PhotoBox objects. I want the images to be close to each other. In order not to forget, I already tried PrefSize () and it does not work.
Using WrapLayout / FlowLayout

Edit:
Now I feel that the problem is related to BoxLayout PhotoBox.
source
share