« Return to Thread: swingbuilder - linking actions to events

Re: swingbuilder - linking actions to events

by shemnon :: Rate this Message:

Reply to Author | View in Thread

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

 « Return to Thread: swingbuilder - linking actions to events