This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala Swing & Function Literals

3 replies
Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
I'm confused about how to use Scala Swing elegantly.  I'd like my client code to look something like:

add(new MyButton("Press Me",println("button was pressed")));

The closest I can get is:
  class MyButton(text: String, actionListener: () => Unit) extends JButton(text) {
    addActionListener(new ActionListener() {
      def actionPerformed(ae: ActionEvent) = actionListener()
    })
  }
  addControl(new MyButton("Press Me", () => println("hello")))
Here are my main questions:

1. How to get rid of the ()=> in the client code function literal?
2. How would this be done with scala.swing reactions?
3. Can I get rid of the intermediate MyButton class?
4. Is there a better way to be doing this?

I don't see the advantage of using matching here (as seems to be recommended in scala swing); I just want the action to always be performed on button press, and to ignore all other event types.

Thanks,
Sam Reid
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala Swing & Function Literals

    def button(text : String, fun : => Any) =
    {
        val but = new JButton(text);
        but.addActionListener(new ActionListener {
                override def actionPerformed( ae:ActionEvent) = fun
            });
        but
    }

addControl button("mytext",println("shit happened"))

works for you?

On Fri, Jan 16, 2009 at 5:30 PM, Samuel Robert Reid <Reids@colorado.edu> wrote:
I'm confused about how to use Scala Swing elegantly.  I'd like my client code to look something like:

add(new MyButton("Press Me",println("button was pressed")));

The closest I can get is:
  class MyButton(text: String, actionListener: () => Unit) extends JButton(text) {
    addActionListener(new ActionListener() {
      def actionPerformed(ae: ActionEvent) = actionListener()
    })
  }
  addControl(new MyButton("Press Me", () => println("hello")))
Here are my main questions:

1. How to get rid of the ()=> in the client code function literal?
2. How would this be done with scala.swing reactions?
3. Can I get rid of the intermediate MyButton class?
4. Is there a better way to be doing this?

I don't see the advantage of using matching here (as seems to be recommended in scala swing); I just want the action to always be performed on button press, and to ignore all other event types.

Thanks,
Sam Reid



--
Viktor Klang
Senior Systems Analyst
Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
Re: Scala Swing & Function Literals
Yes, this works great.  I guess you just have to be consistent about whether you put the () at the fun declaration, fun call and function literal.

Thanks,
Sam Reid

Viktor Klang wrote:
4d0f9db0901160844m10fffe49ie3ba919a744e93b2 [at] mail [dot] gmail [dot] com" type="cite">
    def button(text : String, fun : => Any) =
    {
        val but = new JButton(text);
        but.addActionListener(new ActionListener {
                override def actionPerformed( ae:ActionEvent) = fun
            });
        but
    }

addControl button("mytext",println("shit happened"))

works for you?

On Fri, Jan 16, 2009 at 5:30 PM, Samuel Robert Reid <Reids [at] colorado [dot] edu" rel="nofollow">Reids@colorado.edu> wrote:
I'm confused about how to use Scala Swing elegantly.  I'd like my client code to look something like:

add(new MyButton("Press Me",println("button was pressed")));

The closest I can get is:
  class MyButton(text: String, actionListener: () => Unit) extends JButton(text) {
    addActionListener(new ActionListener() {
      def actionPerformed(ae: ActionEvent) = actionListener()
    })
  }
  addControl(new MyButton("Press Me", () => println("hello")))
Here are my main questions:

1. How to get rid of the ()=> in the client code function literal?
2. How would this be done with scala.swing reactions?
3. Can I get rid of the intermediate MyButton class?
4. Is there a better way to be doing this?

I don't see the advantage of using matching here (as seems to be recommended in scala swing); I just want the action to always be performed on button press, and to ignore all other event types.

Thanks,
Sam Reid



Andrei Formiga
Joined: 2009-02-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Swing & Function Literals

Old thread, but here's how this would be done using the scala.swing
button instead of the native Java swing JButton:

def button(s: String, f: => Any) = new Button(s) {
reactions += {
case ButtonClicked(_) => f
}
}

And yes, this uses pattern matching.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland