Scala Swing - how to react to component resize?


#1

Hello,
I can’t find any information on how to handle component resize (e.g. Panel) in Scala Swing.
Is there an event I can listen to or a function I can override to get new component size?
Thanks,

M


#2

(replying again on the web interface, because e-mail system accuses me of sending an “automatically created” mail; could @admins please check this?)

this is one of those cases where scala-swing doesn’t cover all functions
of java-swing. it’s missing a ComponentResized event dispatch.

you can fall back to java.awt.event, though, using peer of the
component you want to track:

import scala.swing._
import java.awt.{event => jae}

new Frame {
  contents = new Button("Hello") {
    peer.addComponentListener(new jae.ComponentAdapter {
      override def componentResized(e: jae.ComponentEvent): Unit =
        println(s"New size: $size")
    })
  }
  pack().centerOnScreen()
  open
}

#3

Years ago I checked out Scala Swing and gave up on it when I realized that
it did not support all Swing components and had no plans on doing so. Has
this improved since then?


#4

I use Scala-Swing throughout a range of desktop applications, and I would say it’s fairly complete, but yes, occasionally you have to fall back to peer and the Java API, but this is not a big issue IMHO.

I think a few of the components that had been missing are part of Scala Swing now. I created my small addition library https://github.com/Sciss/SwingPlus which adds those components that are missing - e.g. Spinner, ScrollBar - along with some extension methods for missing API, and a few useful panel-layouts, e.g. GroupPanel. With this combination, I think Scala Swing is perfectly usable, and I would advise against missing out and going directly to the Java API.

best, .h.h.


#5

I have moved from Scala-Swing to ScalaFX for GUI development, and I am quite happy with that.


#6

I’m now so familiar with web development, even if I create a small
experimental prototype and I want a GUI, I add a web interface.


#7

ScalaFX, like Scala Swing, is for stand alone applications and fat clients, so not web development.
IMO the GUI controls look much better and the API is easier, although I think the JavaFX API did a few things wrong.


#8

Actually - looking into the source code, there is an event UIElementResized.

So this would be the correct way to do it:

import scala.swing._

new Frame {
  contents = new Button("Hello") {
    listenTo(this)
    reactions += {
      case event.UIElementResized(_) =>
        println(s"New size: $size")
    }
  }
  pack().centerOnScreen()
  open
}

#9

Thank you for looking into this. I saw UIElementResized among event types but I added listenTo at the frame level, and that did not work. It’s good to know this is working fine.
Thanks a lot!

M