Kotlin Coroutine Confidence: Placeholder message does not appear (pg 41)

Pg 41 states “Run the code, and you’ll see the placeholder message in a small window, just like last time.” for the code astronomy/v4/src/main/kotlin/com/example/astronomy/AstronomyApplication.kt. However when I run this, I get the a blank JFrame. The “Loading your image…” JLabel does not show.

This is in the B3 version of the book with the example code downloaded on 2025-04-19 at 14:42 EDT.

book-kotlin-coroutine-confidence pragprog

You’re absolutely right, the message is missing here for me too. It must have been working when I originally wrote and tested the code, so I’ll have to go back and see where the problem crept in.

To fix this one, we can add a call to window.revalidate() after adding the JLabel:

class ImageViewer : WindowAdapter() {
  override fun windowOpened(e: WindowEvent) {
    val window = e.window as JFrame
    window.add(JLabel("Loading your image…", JLabel.CENTER))
    window.revalidate()
    // TODO: download and display an image
  }
}

Let me know if this fixes the code for you. I’ll do a little more digging to make sure I understand how I missed the problem. Thank you so much for spotting the issue and letting me know about it!

That works. Before posting my message, I had tried adding window.repaint(), but that didn’t work. I only know basic Swing, so was not familiar with revalidate().

I should note that the same issue is present in astronomy/v7. The "Loading your image…” does not show before the image shows. The issue might be present in later versions as well. But I left off with astronomy/v7. When I quit for the evening, my above post had not completed moderation yet so I could not add a note or reply to it about v7 also having the issue. If I find any others that need the revalidate() call, I’ll post another reply to this thread. (If you’d prefer a new thread, let me know,)

Thanks.

p.s. I really like the way your examples build up by using the different v# versions. Even including the “oops” ones showing a step that doesn’t work. So often the example code for books just show the final version, and you lose the “how did we get here” context, and the “alternative solution” contexts. Being able to see, and have for future study, the incremental versions is suuuuuuuuuuuuper helpful. I’m sure it adds work to the process for you. But it really does make the book that much more awesome. So thanks for that. I’m really enjoying the book so far, and learning a lot.

I can confirm the revalidate() call is also need in:

  • astronomy/v7
  • astronomy/v8
  • astronomy/v9
  • astronomy/v10
  • astronomy/v11
  • astronomy/v12
  • astronomy/v13

Yep, it makes sense that the same issue would be there in the other examples too. Still not sure how it slipped past my defences! Thanks for listing the affected versions—I’ll make sure to fix and test them all. Here’s where I find out if I’ve set up my version control correctly so I can fix the first one and have it propagate through to the others :crossed_fingers:

I’m very happy to hear you’re enjoying the book, and I’m so glad you like the way the code examples are built up in intermediate stages! I spent some time thinking about how to get that right, and got some helpful advice about it from other Pragmatic authors. It’s great to hear that having all the incremental versions is useful.