- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala Swing & Function Literals
Fri, 2009-01-16, 17:31
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:
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
add(new MyButton("Press Me",println("button was pressed")));
The closest I can get is:
class MyButton(text: String, actionListener: () => Unit) extends JButton(text) {Here are my main questions:
addActionListener(new ActionListener() {
def actionPerformed(ae: ActionEvent) = actionListener()
})
}
addControl(new MyButton("Press Me", () => println("hello")))
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
Fri, 2009-01-16, 17:57
#2
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:
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) {Here are my main questions:
addActionListener(new ActionListener() {
def actionPerformed(ae: ActionEvent) = actionListener()
})
}
addControl(new MyButton("Press Me", () => println("hello")))
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
Thu, 2009-02-26, 15:47
#3
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.
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:
--
Viktor Klang
Senior Systems Analyst