sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
494 stars 105 forks source link

Popup or Dialog not showing up #506

Closed f4bsn closed 7 years ago

f4bsn commented 7 years ago

Hi, I'm about to refactor our application to use your mvvm framework. From the MenuBar I was opening a DirectoryChooser or a FileChooser but the according dialogs just don't show up anymore. In my MenuBarViewModel I have the following function which is called via a Command from the MenuBarView:

private void performAddFolder() {
        File dir = new DirectoryChooser().showDialog(stage);
        if (dir != null) {
            searchFoldersForMP3(dir);
        }
    }

Any idea why neither FileChooserDialog nor DirectoryChooserDialog are working?

Thanks, Fabian

manuel-mauky commented 7 years ago

Hi, with "Command" do you mean mvvmFX Commands? If so, can you post the code of how you create and trigger the command?

The code to open the dialog has to be invoked on the JavaFX UI thread. Maybe due to some misconfiguration this isn't the case anymore? You can test this with Platform.isFxApplicationThread() for example in a System.out-Message in your performAddFolder method.

f4bsn commented 7 years ago

Yup, that's what I mean. Here's the code of the Command from my ViewModel class:

@Inject
    private Stage stage;

    private Command addFolderCommand;

    public Command getAddFolderCommand() {
        if (addFolderCommand == null) {
            addFolderCommand = new DelegateCommand(() -> new Action() {
                @Override
                protected void action() throws Exception {
                    performAddFolder();
                }
                // TODO: 21.05.2017 , createAddFolderPossibleBinding() check if music is currently added
            }, true);
        }
        return addFolderCommand;
    }

    private void performAddFolder() {
        System.out.println(Platform.isFxApplicationThread());
        File dir = new DirectoryChooser().showDialog(stage);
        if (dir != null) {
            searchFoldersForMP3(dir);
        }
    }

Platform.isFxApplicationThread() is false so it is the JavaFX UI thread right?

manuel-mauky commented 7 years ago

In the constructor of DelegateCommand you have the last parameter true. This defines this command to be executed on a background thread. This is used to prevent the UI from freezing when long running tasks are executed. To fix this you can either:

protected void action() throws Exception {
    Platform.runLater(() -> performAddFolder());
}

This should fix your code.

f4bsn commented 7 years ago

Perfect, thanks! It did!