Search code examples
javaswingjlabellayout-managerflowlayout

Fill up the empty space of a FlowLayout


I have a flowlayout panel which has a textfield inside. Although the textfield is not that wide but I want to place components on the next line. How do I fill the remaining space with an empty box?


Solution

  • If you need more deterministic control over the way your components are laid out, you should consider using a different layout manager such as BoxLayout, GridLayout, or GridBagLayout. You may be able to trick FlowLayout into doing what you want, but what happens if the user resizes the window? Or changes their font DPI at the OS level? Instead of having your components be on different lines as a side effect of putting dummy components into a FlowLayout, consider expressing the above/below relationship more explicitly with a layout manager that has those concepts built in (e.g. GridBagConstraints.gridy).