0

I have this example where the panels(with title,desc and button) are placed next to one anotherenter image description here

This is the code

 public class Example extends JFrame {

    Example() {

        setLayout(new FlowLayout());
        for (int i = 0; i < 4; i++)
            add(new MyPanel());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class MyPanel extends JPanel {

        MyPanel() {

            setLayout(new BorderLayout());
            add(new JLabel("title", SwingConstants.CENTER), BorderLayout.PAGE_START);
            add(new JLabel("<html>WWWWWW WWWWWWW<p>WWWWWWWWW<p>RRRRRRR RRRRR RRRRRRR"));
            add(new JButton("Details"), BorderLayout.PAGE_END);
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new Example());
    }
}

how to wrap the panels every time?

enter image description here

Fr4ncx
  • 346
  • 4
  • 20

1 Answers1

4

Just use a different layout manager -- please read up on them. A new GridLayout(0, 1) for variable number of rows and 1 column would work. Also consider using a BoxLayout that uses the constraint BoxLayout.PAGE_AXIS.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346