views:

101

answers:

2

The ScalaDoc for the applet class is pretty thin on details on how you actually override the ui piece and add components. It says "Clients should implement the ui field. See the SimpleApplet demo for an example."

  1. Where is this SimpleApplet demo?
  2. Barring that, does anyone have some simple source code of using the Scala Applet class, rather than the JApplet class directly?

Thanks

+2  A: 

Finally found some source that shows what you need to do:

http://scala-forum.org/read.php?4,701,701

import swing._ import java.awt.Color

class MainApplet extends Applet {

  object ui extends UI {
     val mainPanel = new BoxPanel(Orientation.Vertical) {
     // different sort of swing components
     contents.append(new Button("HI"))
     }
     mainPanel.background = Color.WHITE
     contents = mainPanel

     def init():Unit = {}
  }
}

In other words you define an object named ui that extends UI. I never would have thought of that. That ScalaDoc needs some serious work.

I82Much
+3  A: 

The more recent ScalaDoc may be slightly more helpful (in particular, the new version of ScalaDoc allows you to show/hide concrete members so you can focus on what you must implement).

It should be noted that you don't have to define an object named ui that extends UI. What the ScalaDoc says is both more accurate and more flexible -- "implement the ui field". Because of the Uniform Access Principle, you're free to implement the ui field as a val or an object (similarly, you can use a val or var to implement a def). The only constraints (as reflected in the ScalaDoc as val ui : UI) are that

  1. the ui has to be a UI, and
  2. the reference to the ui has to be immutable

For example:

class MainApplet extends Applet {
   val ui = new MainUI(Color.WHITE)
}

class MainUI(backgroundColor: Color) extends UI {
   val mainPanel = new BoxPanel(Orientation.Vertical) {
      // different sort of swing components
      contents.append(new Button("HI"))
   }
   mainPanel.background = backgroundColor // no need for ugly _=
   contents = mainPanel

   def init(): Unit = {}
}
Aaron Novstrup
Excellent information. Thank you
I82Much
One minor correction: `def` can be overridden with `val`, but not the other way around.
Daniel
@Daniel -- I surprised myself when I "proved" you could override a `val` with a `def` in the interpreter. Turned out I had forgotten to extend the trait that defined the val. :) I'm glad that's not actually possible!
Aaron Novstrup