Search code examples
swingscalawrapperjinternalframescala-swing

JInternalFrame with scala.swing


scala swing looks interesting, but somehow it is incomplete, and sometimes I still need to use the old java classes, but I have no clue how I have to wrap them correctly.

So how do I wrap javax.swing.JInternalFrame correctly so that I can use it as Component in my MainFrame?

I try to get this example to work with scala and the scala swing library, and I finally managed to get an Internal Frame, but my MainFrame distorts all internal Frames and stretches them until they have exactly the same width and height as the space inside the MainFrame.

This is my current implementation:

import swing._
import event._

object InternalFrameDemo extends SimpleSwingApplication{

    val top = new MainFrame{
        title = "InternalFrameDemo"
        preferredSize = new Dimension(640,480)

        val menuNew = new MenuItem("New"){
            mnemonic = Key.N
            action = new Action("new"){
                def apply(){
                    createFrame
                }
            }
        }

        val menuQuit = new MenuItem("Quit"){
            mnemonic = Key.Q
            action = new Action("quit"){
                def apply(){
                    quit()
                }
            }
        }

        menuBar = new MenuBar{
            contents += new Menu("Document"){
                mnemonic = Key.D
                contents ++= Seq(menuNew,menuQuit)
            }
        }

        def createFrame{
            val newFrame = MyInternalFrame()
            newFrame.visible = true
            contents = newFrame
        }
    }
}

object MyInternalFrame{
    var openFrameCount = 0;
    val xOffset, yOffset = 30;

    def apply() = {
        openFrameCount += 1
        val jframe = new javax.swing.JInternalFrame("Document #" + openFrameCount,true,true,true,true)

        jframe.setSize(300,300)
        jframe.setLocation(xOffset*openFrameCount,yOffset*openFrameCount)

        Component.wrap(jframe)
    }
}

Solution

  • The following is defined on the companion of Component:

    def wrap (c: JComponent): Component 
    

    So you write Component.wrap(new JInternalFrame).