Search code examples
swingscalaiconsnimbus

Specifying a frame icon on Scala’s Swing Frame


How can you set a new frame icon on Scala’s scala.swing.Frame class? What are the intentions behind Frame.iconImage: Image and Frame.iconify()? I can’t figure out, what they’re doing.

Here’s my last attempt

import scala.swing.Frame

class MyFrame extends Frame {
  iconImage = toolkit.getImage("src/main/resources/icon.png")
  visible = true
}

I also tried several other methods, but nothing worked.


Solution

  • I'm guessing you are on OS X. Sadly, the icon decoration does not work for the OS X look and feel, neither does it work for Nimbus look and feel which seems to not come with a specific window decoration (uses title bar from OS X).

    So you will need a look and feel that does paint its own window title bar:

    import scala.swing._
    import javax.swing._
    UIManager.setLookAndFeel(new plaf.metal.MetalLookAndFeel)
    JFrame.setDefaultLookAndFeelDecorated(true)
    
    val f = new Frame {
       iconImage = toolkit.getImage(new java.net.URL(
          "http://www.scala-lang.org/sites/default/files/favicon.gif"))
       size = new Dimension(200, 200)
       visible = true
    }
    

    The only chance with OS X window title bars is if you want to decorate with the default icon used for a particular file.

    Look for Window.documentFile here: http://developer.apple.com/library/mac/#technotes/tn2007/tn2196.html#//apple_ref/doc/uid/DTS10004439