2

I'm developing a program in JavaFX (more specifically FornadoFX because I write it in Kotlin). What I've noticed is that when main window opens it initially in top left corner and then it jumps to the center.

This is how I start the application: launch<MainWindowClass>(args)

And this is my start method:

override fun start(stage: Stage) {
    with(stage){
        minWidth = 600.0
        minHeight = 250.0

        //Making it appear in the center
        val screenBounds = Screen.getPrimary().visualBounds
        x = screenBounds.width / 2 - minWidth / 2
        y = screenBounds.height / 2 - minHeight / 2
        scene = Scene(Group(), minWidth, minHeight)

        super.start(this)
    }
}

Central part (lines from val screenBounds... to scene = ...) have been based on this answer.

However no matter if they are or not there the window always opens in top left and then jumps to set position, rather then being first shown there in the first place.

EDIT:

This is the minimal working example of the bug:

import javafx.scene.Group
import javafx.scene.Scene
import javafx.stage.Screen
import javafx.stage.Stage
import tornadofx.*

class MainWindow: App(MainView::class) {

    class MainView: View() {
        override val root = label("A window")
    }

    companion object {
        @JvmStatic fun main(args: Array<String>){
            launch<MainWindow>(args)
        }
    }

    override fun start(stage: Stage) {
        with(stage){
            minWidth = 600.0
            minHeight = 250.0

            val screenBounds = Screen.getPrimary().visualBounds
            x = screenBounds.width / 2 - minWidth / 2
            y = screenBounds.height / 2 - minHeight / 2

            scene = Scene(Group(), minWidth, minHeight)

            super.start(this)
        }
    }
}

Also some info about system: Java version: 11 Operating system: Ubuntu 18.04 LTS

Miku
  • 567
  • 6
  • 15
  • Please post a _complete_ runnable app that exhibits this behavior :) – Edvin Syse Aug 09 '19 at 08:06
  • @EdvinSyse I did it. As you can see there is not much going on outside `start` method – Miku Aug 09 '19 at 08:22
  • 1
    See this [answer](https://stackoverflow.com/a/53351433/3956070), not the same issue, but has the same root cause. – José Pereda Aug 09 '19 at 09:21
  • @JoséPereda Thank you. This indeed solved the problem. Thanks :) You can post it in the comment so that I could mark it as a solution – Miku Aug 09 '19 at 09:26
  • Ok, I've added it as an answer, and doing so I've noticed that the issue might have been already solved, so maybe you could try JavaFX 13-ea+11? – José Pereda Aug 09 '19 at 09:47

1 Answers1

4

Different issues have been reported for Linux and JavaFX 11, and some of them share the same root cause: the change from GTK 2 to GTK 3.

This answer already explains it in detail.

As for the Windows issue, it has been already filed here, and it has been fixed, so probably you could try JavaFX 13-ea+11 to test it.

In the meantime, or if you have to stick to a released JavaFX 11/12 version, the only workaround is the one suggested in the mentioned answer: run the app with GTK 2, that can be set with the system property:

java -Djdk.gtk.version=2
José Pereda
  • 44,311
  • 7
  • 104
  • 132