I've changed b to Button, but the error changed to

java.lang.IllegalArgumentException: display is null.

as the display is not initialized yet. Namely, I'm doing this during startup() to define listeners. The method is called once, but as it is called for each button I wanted to reduce the number of lines ;-) by writing a one-liner for each button, and maybe further on, put all the buttons in a collection and iterate over its members.
It is just reducing the number of lines in code.


Greg Brown said the following on 23/11/2010 19:10:
I noticed that, in the second example, you are passing an instance of PushButton as b rather than Button. That may be causing the problem, since the buttonPressed() method takes an instance of Button, not PushButton.

Also, I'm not sure how you expect to call pressedDo(), but as coded it will add a new event listener every time it is called. Is that the intended behavior?


On Nov 23, 2010, at 1:05 PM, Bojan Vučinić wrote:

Hi All,

If you are using Scala for your code then you are obliged to write listeners in the following way:

printButton.getButtonPressListeners.add(new ButtonPressListener {
      override def buttonPressed(b: Button) {
        print
      }
   })
I've tried to simplify this by defining the following method:
   def pressedDo (aB: PushButton, action: Unit) = {
     aB.getButtonPressListeners.add(new ButtonPressListener {
         override def buttonPressed(b: PushButton) { action }})
   }
and than invoking the method in this way (example)
     pressedDo(printButton, print)
however, this results in a compiler error:
error: object creation impossible, since method buttonPressed in trait 
ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not 
defined
     aB.getButtonPressListeners.add(new ButtonPressListener {
I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??

Best regards,
Bojan


Reply via email to