Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 542
Ali Avatar asked Jan 04 '14 17:01

Ali


People also ask

What is HBox JavaFX?

The JavaFX HBox component is a layout component which positions all its child nodes (components) in a horizontal row. The Java HBox component is represented by the class javafx. scene. layout. HBox .

What is HBox and VBox in JavaFX?

The layout panes HBox and VBox are definitely the most basic layout containers in JavaFX 2.0. As you can already tell by their name, their purpose is to layout all their children in one horizontal row ( HBox ) or in one vertical column ( VBox ).

Which one of the following panes organizes the nodes in row by row horizontally or column by column vertically?

The TilePane layout pane places all of the nodes in a grid in which each cell, or tile, is the same size. Nodes can be laid out horizontally (in rows) or vertically (in columns).

What is the difference between HBox and VBox pane?

HBox is a subclass of Pane that lays out its children next to each other, in a horizontal row. VBox is a subclass of Pane that lays out its children in vertical column. An HBox might be used as the bottom component in a BorderPane, making it possible to line up several components along the bottom of the border pane.


1 Answers

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)

like image 145
doomsdaymachine Avatar answered Nov 12 '22 05:11

doomsdaymachine