Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager
may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container
(such as Panel
), it should be possible to have a Panel
which allows line breaks.
The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container
.
The behavior of the FlowLayout
(the default Layout Manager for Panel
) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.
Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component
that represents a line break, which can be added to a Container
by using the add()
method.
For example, have a class constant Component
in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK
, and adding that to the Container
can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:
Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));
The above hypothetical LineBreakLayout
will then render the first Label
in one line and the second Label
in the second line.