2

I'm using JavaFX-8 to build an application. I'm trying to build an menue. Each menu item is a ToggleButton all the ToggleButtons should have the same width. So I used following code snipped:

        VBox vbox = new VBox();
        ToggleButton toggleButton = new ToggleButton("MyText");
        HBox hBox = new HBox();
        hBox.getChildren().add(toggleButton);
        HBox.setHgrow(toggleButton, Priority.ALWAYS);
        toggleButton.setMaxWidth(Double.MAX_VALUE);
                            [...]
        vbox.getChildren().add(hBox,hbox2,hbox3,...);

Result:

snipped of the result of the shown code

The width of each item is now the same, but the text alignment seems to be centred now. I tried to change the alignment to LEFT by toggleButton.setContentDisplay(ContentDisplay.LEFT) and toggleButton.setAlignment(Pos.CENTER_LEFT) but without success. Is there an other way to solve this problem?

Update: I tried toggleButton.setTextAlignment(TextAlignment.LEFT); having the same result as before.

Community
  • 1
  • 1
HW90
  • 1,953
  • 2
  • 21
  • 45

2 Answers2

2

There are various mechanisms for controlling alignment in Labeled and its subclasses:

The alignmentProperty() is the one you want here. According to the documentation, it:

Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.

So you just need

toggleButton.setAlignment(Pos.CENTER_LEFT);

The textAlignmentProperty

Specifies the behavior for lines of text when text is multiline Unlike contentDisplayProperty() which affects the graphic and text, this setting only affects multiple lines of text relative to the text bounds.

In other words, if you have multiple lines of text, you can think of the entire text as taking up a rectangle whose height is bounded by all the lines and whose width is bounded by the widest line. The textAlignment property specifies how each line is aligned within that rectangle. That doesn't apply in your case, since you only have one line of text.

Finally, there is the contentDisplayProperty(). This

Specifies the positioning of the graphic relative to the text.

and so only makes a difference if you have both text and graphic displayed in the button. Setting it to ContentDisplay.LEFT specifies that the graphic should be placed to the left of the text, for example.

James_D
  • 201,275
  • 16
  • 291
  • 322
0
ToggleButton toggleButton = new ToggleButton("MyText");
HBox hBox = new HBox();
hBox.getChildren().add(toggleButton);
HBox.setHgrow(toggleButton, Priority.ALWAYS);
toggleButton.setMaxWidth(Double.MAX_VALUE);
toggleButton.setTextAlignment(TextAlignment.LEFT);//set text align

See JavaDoc ToggleButton :https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToggleButton.html and TextAlignment http://download.java.net/jdk9/jfxdocs/javafx/scene/text/TextAlignment.html#LEFT

Option II
Using JavaFX style sheet

.toggle-button1{
    -fx-font: 30 arial; 
    -fx-text-alignment: left;   
}

In Java

scene.getStylesheets().add("myStyle.css");//load css
toggleButton.getStyleClass().add("toggle-button1");// assign style
Danielson
  • 2,605
  • 2
  • 28
  • 51
  • It has been a while for me, progging JavaFX. But I thought there was something like `redraw`, `invalidate` to call after a GUI update is made... (looking for it now, not finding it yet),,, after your update, my answer looks stupid :( – Danielson Jul 27 '15 at 16:16
  • @HW90 how about a css? – Danielson Jul 27 '15 at 16:24
  • @Danielson there are no `redraw` or `invalidate` methods in JavaFX similar to the ones you are looking for. The scene will repaint automatically if you change the properties. – James_D Jul 27 '15 at 17:36
  • Tried my **option 2**? – Danielson Jul 27 '15 at 17:41
  • `textAlignment` is the wrong property (see the documentation); so it doesn't really matter if you set it in Java code or in CSS. – James_D Jul 27 '15 at 18:07
  • @Danielson: Yes tried it, but it does not change anything – HW90 Jul 27 '15 at 21:32