2
votes

This is a really strange bug, but anytime I try to put text with underscores on any JavaFX node besides Labels (Buttons, Checkboxes, etc) the first underscore is never rendered.

Simple example to illustrate my point:

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);
            TilePane tp = new TilePane();
            root.setCenter(tp);

            for (int i = 0; i < 10; i++) {
                Button b = new Button("Underscores_Do_Not_Show_Right");
                tp.getChildren().add(b);
            }

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Looks like this: The first underscore is missing for each button

This seems like a JavaFX bug, but does anyone know of a workaround? For Checkboxes I simply put a Label next to a text-less Checkbox. With Buttons, however, this is not an option.

1

1 Answers

7
votes

Since Buttons have a mnemonicParsingProperty, you should disable that property to avoid underscores being interpreted as mnemonics. Like so:

b.setMnemonicParsing(false);