Unable to add components on a new line in this JPanel

30 Views Asked by At

I'd like to create in Java Swing a file manager-alike interface, with icons spanning horizontally unless no more horizontal space is possible to allocate, and then going to a new line.

This is my current code:

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(156, 36, 1098, 634);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    getContentPane().add(scrollPane);
    
    DesktopPanel = new JPanel();
    scrollPane.setViewportView(DesktopPanel);
    DesktopPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

So I perform some DesktopPanel.add(component); but the components span always horizontally even when there's no more space, so I see the components to go off the screen. I suspect that's because the FlowLayout "thinks" there's more space because it's contained in a JScrollPane which can have infinite horizontal space, but I do not know how I can confine that space to the visible space only.

Thank you in advance.

0

There are 0 best solutions below