swingbuilder - linking actions to events

View: New views
4 Messages — Rating Filter:   Alert me  

swingbuilder - linking actions to events

by Glen Pepicelli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi all,

  How do I attach a Action to one of the many events swing has besides the obvious ones that are referred to by "action" in swingbuilder?

  I figured out (by guessing) one way below but my syntax "windowClosing: exit.closure" seems a little odd.  I wanted to make sure there isn't another syntax.  



                def exit= builder.action(
                        name:'Exit', closure: {System.exit(0)}
                        )
                       
                def frame=
                builder.frame(
                                title:'Catalog',
                                windowClosing: exit.closure,
                            size: [screenWidth -50, screenHeight -50],
                                location: [19,4],
                                show: true,
                                )
                {
                    menuBar() {
                        menu(text: "File", mnemonic: 'F') {
                            menuItem(text: "Exit", mnemonic: 'X', action: exit )
                        }
                    }
                }


Thanks!
Glen

Re: swingbuilder - linking actions to events

by shemnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

javax.swing.Action items do not map nicely to plain old listener interfaces.  You don't need an action object for those but instead all you really need is the closure itself.  The Action is meant for items that share common mnenomics, like buttons and menubar items, and the list is fairly limited.

If you need to share the aciton with other buttons I would recommend storing the closure separate from the action:


def exitClosure = {System.exit}

def exit= builder.action(
       name:'Exit', closure: exitClosure
      )

def frame=
               builder.frame(
                               title:'Catalog',
                               windowClosing: exitClosure,
                           size: [screenWidth -50, screenHeight -50],
                               location: [19,4],
                               show: true,
                               )
               {
                   menuBar() {
                       menu(text: "File", mnemonic: 'F') {
                           menuItem(text: "Exit", mnemonic: 'X', action: exitAction )
                       }
                   }
               }

in fact, if you are note re-using the action across multiple widgets (and have no plans to do such) you may as well not do the action and load the values directly into the menuItem.

On Sat, Jun 27, 2009 at 9:33 PM, Glen Pepicelli <glenbrent@...> wrote:


Hi all,

 How do I attach a Action to one of the many events swing has besides the
obvious ones that are referred to by "action" in swingbuilder?

 I figured out (by guessing) one way below but my syntax "windowClosing:
exit.closure" seems a little odd.  I wanted to make sure there isn't another
syntax.



               def exit= builder.action(
                       name:'Exit', closure: {System.exit(0)}
                       )

               def frame=
               builder.frame(
                               title:'Catalog',
                               windowClosing: exit.closure,
                           size: [screenWidth -50, screenHeight -50],
                               location: [19,4],
                               show: true,
                               )
               {
                   menuBar() {
                       menu(text: "File", mnemonic: 'F') {
                           menuItem(text: "Exit", mnemonic: 'X', action: exit )
                       }
                   }
               }


Thanks!
Glen
--
View this message in context: http://www.nabble.com/swingbuilder---linking-actions-to-events-tp24238420p24238420.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
------------------------------------------------------
"But you didn't." - Jim Halpert, The Office S05E23

Re: swingbuilder - linking actions to events

by Glen Pepicelli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


 I see something that was confusing me... there is both an "action" and "actionperformed".. you can; tpass a closure directly to "action" but you can with "actionperformed":

                    menuBar() {
                        menu (text: "Debug") {
                        menuItem(text: "Open Debug Window")
                                        menuItem(text: "hello", actionPerformed: {println "hi!!"})
                        }
                    }

Glen

Re: swingbuilder - linking actions to events

by Andres Almiray :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, that has to do with Swing's complexity issues. Many components accept an Action property, they also are ActionListeners, meaning that actionPerformed is available via reflection.

You would like to use action: when the intent is to reuse an Action object across two or more components, otherwise actionPerformed: is the way to go.

Cheers,
Andres

Glen Pepicelli wrote:
 I see something that was confusing me... there is both an "action" and "actionperformed".. you can; tpass a closure directly to "action" but you can with "actionperformed":

                    menuBar() {
                        menu (text: "Debug") {
                        menuItem(text: "Open Debug Window")
                                        menuItem(text: "hello", actionPerformed: {println "hi!!"})
                        }
                    }

Glen