Search code examples
javajavafxscenebuilder

JavaFX Alert Dialog


I'm trying to make alert popup in javaFX. However, I can see OK button in English, but CANCEL button was represented in Korean, like this. Please let me know how to make cancel button in English. This is part of my code:

Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Message");
alert.setHeaderText("Are you sure you want to clear all nodes?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get().equals(ButtonType.OK)) { 
       gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
       bst.clear(); 
       height = 20;
       width = 740;
       ratiowidth = 740;
}else if (result.get().equals(ButtonType.NO)) {
       alert.close();
}


Solution

  • welcome to the StackOverflow! :)

    Can you check what is your default locale? Because my default is "en-EN" and out-of-the-box I get value "OK" and "Cancel" buttons. But if I would add that line at the beginning of the application, then I would have similar characters than you, please see attached screenshot.

    Locale.setDefault(new Locale("ja", "Japan"));
    

    enter image description here

    Please run the following code to have buttons with English text inside.

    My code of POC application is:

    Main.java

    package sample;
    
    import javafx.application.Application;
    import javafx.scene.control.Alert;
    import javafx.stage.Stage;
    
    import java.util.Locale;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            //Locale.setDefault(new Locale("ja", "Japan"));
            Locale.setDefault(new Locale("en", "English"));
    
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle("Message");
            alert.setHeaderText("Are you sure you want to clear all nodes?");
        }
    
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    pom.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>al.musi.jacek</groupId>
        <artifactId>javafx-alert-dialog</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-controls</artifactId>
                <version>15.0.1</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
            <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-fxml</artifactId>
                <version>15.0.1</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-maven-plugin</artifactId>
                    <version>0.0.5</version>
                    <configuration>
                        <mainClass>C:\Users\re\Documents\GitHub\javafx-alert-dialog\src\main\java\sample\Main.java</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>