Alex
Alex

Reputation: 41

How to center a flowlayout vertically

I am trying to center a "Flowlayout"-ed JPanel vertically inside another Panel. Here is what I am trying to achieve:

 -----------------
|                 |   
|  one two three  |
|  four five six  |
|  seven eight    |
|  nine           |
|                 |
 -----------------

 ----------------------- 
|                       |   
|  one two three four   |
|  five six seven eight |
|  nine                 |
|                       |
 -----------------------

The content should flow, grow and shrink within the bounds of the containing panel and will also be centered vertically.

Unfortunatly all I could master was a top aligned panel which looks like:

 ----------------------- 
|  one two three four   |
|  five six seven eight |
|  nine                 |
|                       |    
|                       |
 -----------------------
public class ListFlowCell extends JPanel
{

    public ListFlowCell(List<String> list)
    {
            
        setLayout(new FlowLayout(FlowLayout.LEADING));  
        list.forEach(s -> add(new JLabel(s)));
    
    }

}

Is there a way to achieve the desired behaviour with MigLayout?

Upvotes: 1

Views: 1054

Answers (1)

camickr
camickr

Reputation: 324098

I am trying to center a "Flowlayout"-ed JPanel vertically inside another Panel

The wrapper panel can use a GridBagLayout.

By default any component added to the panel will be centered both vertically and horizontally. So you will also need to set the weightx constraint when you add the panel using the FlowLayout which will allow the panel to grow horizontally.

Upvotes: 3

Related Questions