Cannot set BoxLayout (JPanel) to GridLayout / FlowLayout correctly

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));
        //setPreferredSize(new Dimension(200,150));

        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

GridLayout Gap error

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

enter image description here

Edit:

Now I feel that the problem is related to BoxLayout PhotoBox.

+4
source share
3 answers

Please check the image height, because the layout is correct AFAICS, there is nothing else .: D

+2

GridLayout. . GridLayout.

, . GridBagLayout, , .

Wrap Layout Rob Camick. FlowLayout, , , , .

.

MigLayout

+2

GridLayout , . , , title , ​​, Gridlayout .

:

  • , .
  • JPanel FlowLayout, JPanel " " "title"
+2

Source: https://habr.com/ru/post/1523766/


All Articles