14

I have panel which is using flow layout.

How can I make break in flow layout? Like <br/> in html. Some special break element or another trick to indicate that specified element and all subsequent have to go to the next line.

michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

5 Answers5

14

In a case like this, I'd put two containers with flowlayout one on top of each other inside a BoxLayout. Nesting layouts is fairly inexpensive.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
12

The Wrap Layout might be a solution for you. It automatically moves components to the next line when a line is full.

camickr
  • 321,443
  • 19
  • 166
  • 288
8

You want to manually divide the components in multiple rows? So you know where you want the linebreak to be.

In that case I would use 3 panels:

  • 1 panel containing the other 2 panels with a GridLayout with 1 column
  • 2 panels inside the GridLayout, each with a FlowLayout

Example code:

    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    {
        panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);
        panel.setLayout(new GridLayout(0, 1, 0, 0));
        {
            panel_1 = new JPanel();
            panel.add(panel_1);
            {
                lblPanelFlowlayout = new JLabel("Panel 2: FlowLayout");
                panel_1.add(lblPanelFlowlayout);
            }
        }
        {
            panel_2 = new JPanel();
            panel.add(panel_2);
            {
                lblPanel = new JLabel("Panel 3: FlowLayout");
                panel_2.add(lblPanel);
            }
        }
    }

You can add as many new Panels with a FlowLayout as you want. Each time you would do a BR you now set the next panel as active (possibly creationg it dynamically).

extraneon
  • 23,575
  • 2
  • 47
  • 51
1

I don't think that is possible in a flow layout, you might want to try another layout like GridLayout or GridBagLayout

RMT
  • 7,040
  • 4
  • 25
  • 37
0

I'd make multiple placeholder panels with no insets, and then use some code to work out when a component needs to be moved to the next panel because it's below a minimum width threshold. It's ugly, but it should work. You'd need to do all the removal and addition by hand, and within the EDT.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84