JavaFX -- after modal dialog cursor left in incorrect state and cannot be corrected

392 Views Asked by At

I have a modal dialog that I pop up, which contains text boxes. If the user has the mouse over the text box (so the TEXT cursor shows) and then hits Enter to close the dialog, the cursor gets left in the TEXT state. So far I have found no way programmatically to change it back to the DEFAULT state. If I try some other cursor on the main scene (like HAND or MOVE) it works. But DEFAULT does not.

I'm using JavaFx / JDK 1.7 that was "fresh" when I downloaded within the last couple of months. I'm on Windows 8.1.

I've searched the web a fair bit and haven't found any mention of it. Is this a known bug? Can anybody suggest a workaround or explain what is going on?

Code sample follows. Simply click on the text edit in the dialog, keep the mouse over the textbox so the cursor is TEXT, and hit Enter.

package jfxtest;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Jfxtest extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override public void start(final Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open Dialog");
        btn.setOnAction(
            new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent event) {
                    final Stage dialog = new Stage();
                    dialog.initModality(Modality.APPLICATION_MODAL);
                    dialog.initOwner(primaryStage);

                    final TextField txt = new TextField("Hit Enter while I have focus and the mouse is over me!");
                    txt.setMinWidth(280);

                    Button btnOk = new Button("OK");
                    btnOk.setDefaultButton(true);
                    btnOk.setOnAction(
                        new EventHandler<ActionEvent>() {
                            @Override public void handle(ActionEvent event) {
                                // these don't work
                                txt.setCursor(Cursor.DEFAULT);
                                dialog.getScene().setCursor(Cursor.DEFAULT);
                                primaryStage.getScene().setCursor(Cursor.DEFAULT);

                                dialog.close();

                                // and these don't work
                                txt.setCursor(Cursor.DEFAULT);
                                dialog.getScene().setCursor(Cursor.DEFAULT);
                                primaryStage.getScene().setCursor(Cursor.DEFAULT);
                            }
                        } );

                    VBox dialogVbox = new VBox(20);
                    dialogVbox.getChildren().add(txt);
                    dialogVbox.getChildren().add(btnOk);

                    Scene dialogScene = new Scene(dialogVbox, 300, 200);
                    dialog.setScene(dialogScene);
                    dialog.showAndWait();

                    // and these doesn't work
                    dialogScene.setCursor(Cursor.DEFAULT);
                    primaryStage.getScene().setCursor(Cursor.DEFAULT);

                    // but this works as expected (!), although it doesn't solve my problem
//                  primaryStage.getScene().setCursor(Cursor.HAND);
                }
            } );

        VBox vbox = new VBox(20);
        vbox.getChildren().add(btn);

        Scene scene = new Scene (vbox, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
0

There are 0 best solutions below