Revision: 42947
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 14, 2011 01:58 by laurenceosx
Initial Code
import java.awt.Toolkit; import java.awt.AWTEvent; import java.awt.event.AWTEventListener; import java.awt.event.MouseListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.SwingUtilities; import javax.swing.SwingUtilities as SU; /* Laurence Toenjes 2011-03-13 This is a "crude" global JButton fix click solution, because: Sometimes when using internal frames, it takes two clicks on a button to get the actionPerformed code to fire. How to use (one call in app startup code): FixButtonClick.init() */ /* development settings to relax java security file: ${user.home}/.java.policy grant codeBase "file:///-" { permission java.security.AllPermission "java.security.AllPermission.*"; }; grant codeBase "http://localhost/-" { permission java.security.AllPermission "java.security.AllPermission.*"; }; */ class FixButtonClick { static private initialized = false; static synchronized def fixButtonClick( aButton ) { def sKey = 'has_fixButtonClick'; def getValue = { if ( aButton.getClientProperty( sKey ) == null) aButton.putClientProperty( sKey, false ); aButton.getClientProperty( sKey ) } def hasFix = getValue() == true; if (hasFix) return; aButton.addMouseListener( new MouseAdapter() { void mousePressed(MouseEvent e) { if ( e.source.hasFocus() == false ) { e.source.doClick() } } // end mousePressed } // end MouseAdapter ); aButton.putClientProperty( sKey, true ); } static synchronized def init() { if ( FixButtonClick.initialized == true ) return; FixButtonClick.initialized = true; // Global Event Listener def gel = new AWTEventListener() { void eventDispatched(AWTEvent event) { def isJButton = event.source instanceof javax.swing.JButton; def isMouseEvent = event instanceof java.awt.event.MouseEvent; if (isJButton && isMouseEvent) { def MOUSE_EN = java.awt.event.MouseEvent.MOUSE_ENTERED def isMouseEntered = event.getID() == MOUSE_EN; if ( isMouseEntered ) { def r = new Runnable() { public void run() { FixButtonClick.fixButtonClick( event.source ); } }; if ( SU.isEventDispatchThread() ) { r.run(); } else { SU.invokeAndWait( r ); } } // isMouseEntered } // isJButton && isMouseEvent } } def tk = Toolkit.getDefaultToolkit(); def long mask // mask = Long.MAX_VALUE; // all events mask = AWTEvent.MOUSE_EVENT_MASK; tk.addAWTEventListener( gel, mask as long ); } // end init } // end class
Initial URL
Initial Description
Initial Title
Groovy Swing Fix Button Click : Ensure a single click fires actionPerformed
Initial Tags
Initial Language
Groovy