Popup Menus in Groovy

While trying to figure out how to create a Popu Menu in Groovy, I started my search from google searching for PopupMenu, JPopupMenu tutorial etc. Some of the results were the very good tutorial How to Use Menus in Java and the An example of the JPopupMenu in action, but it was not easy to translate/rewrite the Java examples in Groovy.

Searching about popup menus and groovy gave no fruits.

Rethinking about my request, I understood that the problem was I had no idea how to handle, the Groovy way, the MouseListeners that would give me the ability to show the popup menu.

Finally while searching about Groovy and MouseListener I found this thread at MarkMail with an answer from Erik Husby and two other versions from Danno Ferrin. I had all the needed things for my popup menu

These are two compact Groovy scripts for a menu and a popup menu.


The smallest code for a menu

Very compact Groovy : a frame with an exit on menu bar

new groovy.swing.SwingBuilder().frame(title:'v1.0',pack:true,show:true){
    menuBar {
        menuItem{
            action(name:'Click here to Exit', closure:{System.exit(0)})
        }
    }
}

The smallest code for a popup menu

Very compact Groovy : a frame with an exit on popup menu

new groovy.swing.SwingBuilder().frame(title:'v1.1',pack:true,show:true){
    panel(mousePressed: {e->
        if (e.isPopupTrigger()) {
            popupMenu {
                menuItem{
                    action(name:'Exit', closure:{System.exit(0)})
                }
            }.show(e.getComponent(), e.getX(), e.getY())
        }
    }){
        label("Right click here to get a popup menu to Exit")
    }
}

These are some other scripts for popup menu in Groovy.


A frame with menu and a call of dialog box

A separator in the first level acts like a spring between the menu items
Mnemonics can be used for keyboard access (Alt-Mnemonic)
A predefined closure with a dialog box can used as menuItem closure

import groovy.swing.SwingBuilder

def frame
swing = new SwingBuilder()

showAbout = {
    def pane = swing.optionPane(message:'Testing Menus v1.2')
    def dialog = pane.createDialog(frame, 'About Dialog')
    dialog.show()
}

frame = swing.frame(title:'Main Frame',location:[100,100],size:[500,150]){
    menuBar {
        menu(text:'File', mnemonic:'F') {
            menuItem{
                action(name:'Exit', mnemonic:'E', closure:{System.exit(0)})}
        }
        separator()
        menu(text:'Help', mnemonic:'H') {
            menuItem(
                action(name:'About', mnemonic:'A', closure:{showAbout()})}
        }
    }
    panel {
        label(text: "A label with a message")
    }
}
frame.show()

A frame with a full menu and a same popup menu

For the Popup Menu we need to
– store the menuItems in a closure (we need to use it twice)
– make the frame a MouseListener
Note: menuBar uses parentheses to incorporate the menuItems

import groovy.swing.SwingBuilder

def frame
swing = new SwingBuilder()

def showAbout = {
    def pane = swing.optionPane(message:'Testing Menus v1.3')
    def dialog = pane.createDialog(frame, 'About Dialog')
    dialog.show()
}

def menuItems = {
    menu(text:'File', mnemonic:'F') {
    menuItem() {
        action( name:'Exit', mnemonic:'E', closure:{ System.exit(0) } ) }
    }
    separator()
    menu(text:'Help', mnemonic:'H') {
        menuItem() {
            action(name:'About', mnemonic:'A', closure: { showAbout() } ) }
    }
}

frame=swing.frame(title:'Main Frame v1.4',location:[100,100],size:[500,150]){
    menuBar ( menuItems )
    panel {
        label(text: "A label with a message")
    }
}

frame.addMouseListener( { e->
    if (e.isPopupTrigger())
        swing.popupMenu(menuItems).show(e.getComponent(), e.getX(), e.getY());
} as java.awt.event.MouseListener);

frame.show()

This is a very simple way to have popup menus.
This is the Groovy way for a popup menu.

1 σχόλιο »

  1. James said

    Cool. Thanks.

RSS feed for comments on this post · TrackBack URI

Σχολιάστε

Εισάγετε τα παρακάτω στοιχεία ή επιλέξτε ένα εικονίδιο για να συνδεθείτε:

Λογότυπο WordPress.com

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό WordPress.com. Αποσύνδεση / Αλλαγή )

Φωτογραφία Twitter

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Twitter. Αποσύνδεση / Αλλαγή )

Φωτογραφία Facebook

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Facebook. Αποσύνδεση / Αλλαγή )

Φωτογραφία Google+

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Google+. Αποσύνδεση / Αλλαγή )

Σύνδεση με %s

Αρέσει σε %d bloggers: