Tornadofx: Where can the title be set for an App / View?

Created on 22 Feb 2018  路  10Comments  路  Source: edvin/tornadofx

I tried doing it in my main function:

fun main(args: Array<String>) {
    Application.launch(MyApp::class.java, *args)
}

class MyApp : App(MyView::class) {

    override fun start(stage: Stage) {
        stage.title = "foo"
        super.start(stage)
        // ...
    }
}

but it does not work. If I do it after super.start(stage) I get an exception because of the one-way binding of the title. I've checked the relevant parts of App.kt:

    override fun start(stage: Stage) {
        FX.registerApplication(scope, this, stage)

        try {
            val primaryViewType = determinePrimaryView()
            val view = find(primaryViewType, scope)

            @Suppress("UNCHECKED_CAST")
            (view as? Workspace)?.let { FX.defaultWorkspace = primaryViewType as KClass<Workspace> }

            stage.apply {
                // ..
                titleProperty().bind(view.titleProperty) // <-- this creates a one-way binding
                // ...
            }
            // ...
    }

and it seems that TornadoFX creates a binding between the main view's title and the stage's title.
I debugged this and if I set the title for MyView I can see its value here but it won't be set because of the JavaFX implementation for the title property:

    public final StringProperty titleProperty() {
        if (title == null) {
            title = new StringPropertyBase() {

                @Override
                protected void invalidated() {
                    if (impl_peer != null) {         // <-- impl_peer is null here!
                        impl_peer.setTitle(get());
                    }
                }

                // ...
            };
        }
        return title;
    }

So the problem is that I can't set the title by hand because of the binding created in App.kt but I can't set it from the View either because of this null check.

Where can I set the title for my App/View? I checked the documentation but there is no mention of this topic.

Most helpful comment

This is not the issue you described :) Your MainView title is set correctly. To fix the MacOS specific menu bar issue you can consider packaging your application using JavaPackager or FXLauncher - that will take care of the menubar issue.

All 10 comments

The title for the App will be the title for the main view, so simply assign whatever title you want to the main view. This can be done in two ways.

  1. Via the View constructor:
class MyView : View("My app title")
  1. Via manual assignment in init, onDock or any other place you see fit:
class MyView : View() {
    ...
    init {
        title "My app title"
    }
}

I've added a screenshot of the problem:

image

What you see here is the top bar of macOS and my java app window's top. So if I set the title in my view I see that on the top of the window but in the macOS top bar I see java. How do I set something else there?

For example, when I run IDEA I see this:

image

This does not work, I just tried it.

Are you able to do this in a basic JavaFx application or is this just an issue with TornaodFx?

This is not the issue you described :) Your MainView title is set correctly. To fix the MacOS specific menu bar issue you can consider packaging your application using JavaPackager or FXLauncher - that will take care of the menubar issue.

Okay, I will do that. Do you know about a packaging solution for Gradle?

You can execute javapackager from Gradle, and FXLauncher also has a Gradle plugin :)

Cool, I'll take a look at that.

I was able to fix this on my own application by unbinding the titleProperty and resetting it after:

class Application : App(UploadView::class, Styles::class) {
   override fun start(stage: Stage) {
        super.start(stage)
        stage.titleProperty().unbind()
        stage.title = "App Title"
        stage.icons.add(Image("icon.png"))
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

leder11011 picture leder11011  路  5Comments

Leo-Thura picture Leo-Thura  路  6Comments

spkingr picture spkingr  路  4Comments

ThraaxSession picture ThraaxSession  路  3Comments

Mosch0512 picture Mosch0512  路  4Comments