Scala simpleswingapplication - listen to a button with no name (noob)

50 views
Skip to first unread message

gona...@gmail.com

unread,
May 15, 2015, 12:46:53 PM5/15/15
to scala...@googlegroups.com

In simpleSwingApplication, I notice that you can create a button without a name in an initialize context, e.g.:

import scala.swing._
object SimpleSwingApp extends SimpleSwingApplication {

  override def main(args: Array[String]) = super.main(args)

  // top is a required method
  def top = new MainFrame {

    println("Starting")

    title = "First Swing App"

    contents = new Button { text = "Click this" }
  }
}

So how do you add a listenTo routine in the context above (i.e. without a name) so it does something when you click the button?

And what is this method of non-declaring buttons called, rather than specifically declaring Button ? Is it  "anonymously"?


Thanks

Oliver Ruebenacker

unread,
May 15, 2015, 1:03:05 PM5/15/15
to gona...@gmail.com, scala-user

     Hello,

On Fri, May 15, 2015 at 12:23 PM, <gona...@gmail.com> wrote:

In simpleSwingApplication, I notice that you can create a button without a name in an initialize context, e.g.:

import scala.swing._
object SimpleSwingApp extends SimpleSwingApplication {

  override def main(args: Array[String]) = super.main(args)

  // top is a required method
  def top = new MainFrame {

    println("Starting")

    title = "First Swing App"

    contents = new Button { text = "Click this" }
  }
}

So how do you add a listenTo routine in the context above (i.e. without a name) so it does something when you click the button?


  If you want to use the same object (i.e. your button) twice and you don't want to assign it to a reference (what you call declaration), you can add it to some kind of container or collection, or pass it to methods, functions or blocks that do something with it and then return it.

  But in this case, assigning it to a reference appears to be the preferred solution.
 

And what is this method of non-declaring buttons called, rather than specifically declaring Button ? Is it  "anonymously"?

  The process of replacing references by the object they hold is called "inlining".

  I don't think it makes sense to call it an anonymous object, because when you say 

  val button = new Button {...}

  the name "button" is not the name of the object, but the name of the reference to it. The only really named objects are those created with the object keyword, so in a sense, every other object is anonymous, but no one calls it that way, because that is the normal way objects are.
 
     Best, Oliver


Thanks

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.

gona...@gmail.com

unread,
May 19, 2015, 8:15:31 AM5/19/15
to scala...@googlegroups.com, gona...@gmail.com

OK thanks, still don't quite understand how to add a listento to an "inline" (yay!).
The snippet I posted came from the world wide web.
I'm assuming that this snippet would be built on: so is it possible to see the code on how a listento is created for an inline button?
Just curious. Thanks v much,

gona...@gmail.com

unread,
May 19, 2015, 8:15:32 AM5/19/15
to scala...@googlegroups.com, gona...@gmail.com
Thanks, so how do you add a listento to an inline?
Snippet comes from internet somewhere, so I assume at some stage, developer will add the routine.
Thanks v much.
(this might be a double post because googlegroups is a bit weird)



On Friday, May 15, 2015 at 6:03:05 PM UTC+1, Oliver Ruebenacker wrote:

Oliver Ruebenacker

unread,
May 19, 2015, 1:36:05 PM5/19/15
to gona...@gmail.com, scala-user

     Hello,

  You probably just want to assign the button to a val or var. 

  Or, you retrieve it back from contents (but that's more complicated).

  If for some strange reason, you don't want to assign it to a val or var, you can do something like:

  def makeButtonListenedTo(button: Button) : Button {
    // here goes the listenTo part - my Scala Swing is a bit rusty
    button
  }

  contents = makeButtonListenedTo(new Button { text = "Click this"  })

     Best, Oliver

gona...@gmail.com

unread,
Jun 1, 2015, 7:28:13 AM6/1/15
to scala...@googlegroups.com, gona...@gmail.com

For some reason I struggle to understand googlegroups. I could have sworn I said thanks before.

Thanks a lot.
Reply all
Reply to author
Forward
0 new messages