Peter Penzov
Peter Penzov

Reputation: 1626

Switch checkbox text and component

I want to create checkbox with text in the left side and the checkbox component on the right side. How I can switch their place?

CheckBox cb = new CheckBox("Show on Startup");

Upvotes: 5

Views: 6397

Answers (3)

javaLearner
javaLearner

Reputation: 1001

In JavaFX 8, you can do it like this:

Label lb = new Label("left check");
lb.setGraphic(new CheckBox());
lb.setContentDisplay(ContentDisplay.RIGHT); //You can choose RIGHT,LEFT,TOP,BOTTOM

Upvotes: 11

robodanny
robodanny

Reputation: 96

It would be nice if the CheckBox considered the box as its "content" like some of the other controls based on Labeled. Then the contentDisplayProperty could be set to ContentDisplay.RIGHT to achieve this. A nice side-effect would be that we could change the rendering of the box with a setGraphic() call.

As of my release (1.8 EA b129), CheckBox doesn't work that way.

Upvotes: 3

assylias
assylias

Reputation: 328629

There might be an easier way, but you can use a label and wrap it with the CheckBox in a HBox:

HBox box = new HBox();
CheckBox cb = new CheckBox();
Label text = new Label("Show on Startup");
box.getChildren().addAll(text, cb);
box.setSpacing(5);

Upvotes: 3

Related Questions