2021年4月6日星期二

Replacing file via FileChooser dialog pooches application

Background

Save a file using the JavaFX save dialog the continue using the application. This is using JavaFX 16, Java 16, and Linux/Xfce, but the issue is present in previous versions.

Problem

After choosing to overwrite an existing file, the application is no longer fully responsive: the caret disappears, menu is no longer accessible, etc. Replicate:

  1. Create a new file (e.g., touch $HOME/test.txt).
  2. Run the code.
  3. Click File.
  4. Click Save As.
  5. Double-click test.txt.
  6. Confirm replacing the file.
  7. Click File.

Expected: File menu opens, caret remains visible.

Actual: File menu staunchly rebels, caret goes on vacation.

Jankiest workaround: Relinquish application focus, then return, shake head in confusion (optional).

Code

SSCCE:

import javafx.event.*;  import javafx.scene.*;  import javafx.scene.control.*;  import javafx.scene.layout.*;  import javafx.stage.*;    public final class MainApp extends Application {      public static void main( final String[] args ) {      launch( args );    }      @Override    public void start( final Stage stage ) {      final var root = new Group();      final var scene = new Scene( root, 400, 400 );      final var pane = new BorderPane();      final var menuBar = new MenuBar();      final var menu = new Menu( "File" );      final var file = new MenuItem( "Save As" );      final var textArea = new TextArea();        file.setOnAction( e -> new FileChooser().showSaveDialog( scene.getWindow() ) );        menu.getItems().add( file );      menuBar.getMenus().add( menu );        pane.prefHeightProperty().bind( scene.heightProperty() );      pane.prefWidthProperty().bind( scene.widthProperty() );      pane.setTop( menuBar );      pane.setCenter( textArea );      root.getChildren().add( pane );        stage.setScene( scene );      stage.show();    }  }  

Question

How can the application be fixed? (The bug has been reported, FWIW.)

https://stackoverflow.com/questions/66979424/replacing-file-via-filechooser-dialog-pooches-application April 07, 2021 at 12:07PM

没有评论:

发表评论