How to make FlowLayout add components to the top of a frame instead of the center?

ubiquibacon picture ubiquibacon · Apr 24, 2011 · Viewed 8.7k times · Source

I am using FlowLayout and I want my components to be "flow" from the top left of my frame to the bottom right instead of starting in the center of the screen. GridLayout does this fine, but it re-adjusts the size of my components and I don't like that. I would use GridBagLayout but it is so complicated I wanted to see If it is possible to do what I want with FlowLayout.

Answer

Howard picture Howard · Apr 24, 2011

You can pass it as parameter to the constructor

new FlowLayout(FlowLayout.LEADING);

Edit: After having the code I recognized that the vertical alignment is your issue. You should switch to another layout to fix this, e.g.:

contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));