简体   繁体   中英

Label to the left of checkbox with JFoenix in JavaFX

I am using the JFoenix library for JavaFX, and I wanted to display the check box label to the left of the box.

From (default):

在此处输入图像描述

To:

在此处输入图像描述

Looking at the available methods, this is the only thing that reads as it would work but it does not do anything (I am guessing it is just for other graphics):

checkBox.setContentDisplay(ContentDisplay.LEFT);
// checkBox.setContentDisplay(ContentDisplay.RIGHT);

Is there another method or css styling to get the label on the left? Thank you

Incase someone still facing the same problem, here is the answer

from java code

checkbox.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);

from fxml file

nodeOrientation="RIGHT_TO_LEFT"

You can use a label and wrap it with the jfxCheckBox in a HBox like below simple way:

JFXCheckBox jfxCheckBox = new JFXCheckBox();
HBox hBox = new HBox();
Label label= new Label("CHECK BOX");
hBox.getChildren().addAll(label, jfxCheckBox);
hBox.setSpacing(10);

Other way as:

Label lblCheckbox = new Label("CHECK BOX");
lblCheckbox.setGraphic(new JFXCheckBox());
lblCheckbox.setContentDisplay(ContentDisplay.RIGHT);

You have to adapt or override the layoutChildren() method of the JFXCheckBoxSkin in order to place the checkbox after its label.

Change the x position of the layoutLabelInArea() method such that the label moves/remains to the left. This can be achieved by removing the boxWidth from the xOffset :

layoutLabelInArea(xOffset, yOffset, labelWidth, maxHeight, checkBox.getAlignment());

Similarly, move the checkbox to the right by adding the labelWidth to the xOffset :

positionInArea(box, xOffset+labelWidth, yOffset, boxWidth, maxHeight, 0, checkBox.getAlignment()
                .getHpos(), checkBox.getAlignment().getVpos());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM