How to get JavaFX HBoxes to move items to the next line?


Question

I'm trying to show a HBox which contains a list of checkboxes. Example:

HBox container = new HBox();
for (Weekday day: Weekday.values() )
{
   container.getChildren().add( new CheckBox( day.getName() ) ); 
}

However this shows the days all on one line, as:

Sunday Monday Tuesday Wednesday Thursday Friday Saturday

The window is not big enough to show them all on one line, and so I get a horizontal scrollbar. What I need is to show the days on two lines, e.g:

Sunday Monday Tuesday Wednesday
Thursday Friday Saturday

I've tried container.setPrefWidth() but it just causes it to show Su.., Mo.. to truncate the text rather than moving them to the next line.

1
4
1/4/2014 8:57:18 PM

Accepted Answer

You should try using FlowPane. I tried it and got the result you've been seeking for.

The HBox layout pane provides an easy way for arranging a series of nodes in a single row

...

The nodes within a FlowPane layout pane are laid out consecutively and wrap at the boundary set for the pane. Nodes can flow vertically (in columns) or horizontally (in rows). A vertical flow pane wraps at the height boundary for the pane. A horizontal flow pane wraps at the width boundary for the pane.

source: http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm#CHDGHCDG (its a useful read)

13
4/22/2016 12:21:44 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon