

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
3 messages in org.codehaus.groovy.userRe: [groovy-user] mouselistener example| From | Sent On | Attachments |
|---|---|---|
| Dr. R. Mark Meyer | Nov 15, 2007 9:39 am | |
| Erik Husby | Nov 15, 2007 9:48 am | |
| Danno Ferrin | Nov 15, 2007 11:02 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [groovy-user] mouselistener example | Actions |
|---|---|---|
| From: | Danno Ferrin (dann...@shemnon.com) | |
| Date: | Nov 15, 2007 11:02:30 am | |
| List: | org.codehaus.groovy.user | |
Erik's example showed one importiant fact: if it works in Java it will almost certianly work in Groovy. There are also a few other ways to do this. The event delegates can be set in the attributes of the frame as well. (I'm using method closures for this, but any closure would do)
import groovy.swing.SwingBuilder import java.awt.BorderLayout import javax.swing.BorderFactory import java.awt.event.MouseListener import java.awt.event.MouseEvent import java.awt.event.MouseWheelEvent import java.awt.event.MouseMotionListener import java.awt.event.MouseWheelListener
class MouseExploration {
def labels
def frame def swing
static void main(args) { def mouseExploration = new MouseExploration() mouseExploration.run() }
void run() { labels = [:] swing = new SwingBuilder() frame = swing.frame(title:'Title', location:[100,100], size:[1000, 150], show:true, mouseClicked:this.&displayEvent, mouseEntered:this.&displayEvent, mouseExited:this.&displayEvent, mousePressed:this.&displayEvent, mouseReleased:this.&displayEvent, mouseDragged:this.&displayEvent, mouseMoved:this.&displayEvent, mouseWheelMoved:this.&mouseWheelMoved ) { menuBar { menu(text:'File') { menuItem() { action(name:'Exit', closure:{ System.exit(0)}) } } menu(text:'Help') { menuItem() { action(name:'About', closure:{ showAbout() }) } } } panel(layout:new BorderLayout()) { tableLayout (cellpadding:1){ for ( i in ["Event", "X", "Y", "Button", "Modifiers", "ClickCount" ] ) { tr { td (align:'right', colfill:true){ label(text:"${i}: ") } td(align:'left') { labels[i] = label(); } } }
} } } }
void mouseWheelMoved( MouseWheelEvent e) { labels.each { k, v -> v.text = " "}
labels['Event'].text = "${e.paramString()}" }
void showAbout() { def pane = swing.optionPane(message:'Test of Mouse event handling.') def dialog = pane.createDialog(frame, 'About MouseExploration') dialog.show() }
void displayEvent ( MouseEvent e ) { labels['Event'].text = "${e.paramString()}" labels['X'].text = "${e.x}" labels['Y'].text = "${e.y}" labels['Button'].text = "${e.button}" int i = e.modifiers labels['Modifiers'].text = "${e.getMouseModifiersText(i)}" labels['ClickCount'].text = "${e.clickCount}" } }
Also, since all events are the same you could do a proxy from the closure...
import groovy.swing.SwingBuilder import java.awt.BorderLayout import javax.swing.BorderFactory import java.awt.event.MouseListener import java.awt.event.MouseEvent import java.awt.event.MouseWheelEvent import java.awt.event.MouseMotionListener import java.awt.event.MouseWheelListener
class MouseExploration {
def labels
def frame def swing
static void main(args) { def mouseExploration = new MouseExploration() mouseExploration.run() }
void run() { labels = [:] swing = new SwingBuilder() frame = swing.frame(title:'Title', location:[100,100], size:[1000, 150] ) { menuBar { menu(text:'File') { menuItem() { action(name:'Exit', closure:{ System.exit(0)}) } } menu(text:'Help') { menuItem() { action(name:'About', closure:{ showAbout() }) } } } panel(layout:new BorderLayout()) { tableLayout (cellpadding:1){ for ( i in ["Event", "X", "Y", "Button", "Modifiers", "ClickCount" ] ) { tr { td (align:'right', colfill:true){ label(text:"${i}: ") } td(align:'left') { labels[i] = label(); } } }
} } } frame.show()
frame.addMouseListener(this.&displayEvent as MouseListener); frame.addMouseMotionListener(this.&displayEvent as MouseMotionListener); frame.addMouseWheelListener( ({ e -> labels.each { k, v -> v.text = " "} labels['Event'].text = "${e.paramString()}" } as MouseWheelListener)); }
void showAbout() { def pane = swing.optionPane(message:'Test of Mouse event handling.') def dialog = pane.createDialog(frame, 'About MouseExploration') dialog.show() }
void displayEvent ( MouseEvent e ) { labels['Event'].text = "${e.paramString()}" labels['X'].text = "${e.x}" labels['Y'].text = "${e.y}" labels['Button'].text = "${e.button}" int i = e.modifiers labels['Modifiers'].text = "${e.getMouseModifiersText(i)}" labels['ClickCount'].text = "${e.clickCount}" } }
I consider the techniques using closures to be 'Groovier,' but old-school Java techniques works just fine as well, that's what's so great about Groovy.
--Danno







