Search code examples
javaswingjpanellayout-managerflowlayout

Put 2 FlowLayouts one under another with a margin


I'm trying to dynamically create 2 FlowLayout instances (one of them has about 50 dynamically created buttons, other has about 10) so that there is a slight gap between them. I'm stuck with this, I tried to use BoxLayout and put flow layouts in it, and then create gap with Box class and its methods, but it didn't work. I tried with BorderLayout, and GridBagLayout, but that didn't work either.

You can see what I'm trying to do on the image below. I would appreciate if anyone has an idea how to do this. The actual question is: How can I create a gap between the first 50 buttons and other 10 buttons, where both groups of buttons have to be set in flow layout.

http://pokit.org/get/?e23233ce68c2b38371faa53ac10b4b3e.jpg


Solution

  • This effect can be achieved by adding an EmptyBorder to each of the containers with FlowLayout.

    Many layouts allow us to set a gap between components, depending on which layout the 2 containers with flow layout are being added to, that might be a possibility here.