Groovy Swing Fix Button Click : Ensure a single click fires actionPerformed


/ Published in: Groovy
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.awt.Toolkit;
  2. import java.awt.AWTEvent;
  3. import java.awt.event.AWTEventListener;
  4.  
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8.  
  9. import javax.swing.SwingUtilities;
  10. import javax.swing.SwingUtilities as SU;
  11.  
  12. /*
  13. Laurence Toenjes 2011-03-13
  14.  
  15. This is a "crude" global JButton fix click solution, because:
  16.  
  17. Sometimes when using internal frames, it takes
  18. two clicks on a button to get the actionPerformed
  19. code to fire.
  20.  
  21. How to use (one call in app startup code):
  22. FixButtonClick.init()
  23. */
  24.  
  25. /*
  26. development settings to relax java security
  27.  
  28. file: ${user.home}/.java.policy
  29.  
  30. grant codeBase "file:///-" { permission java.security.AllPermission "java.security.AllPermission.*"; };
  31. grant codeBase "http://localhost/-" { permission java.security.AllPermission "java.security.AllPermission.*"; };
  32.  
  33. */
  34.  
  35. class FixButtonClick {
  36.  
  37. static private initialized = false;
  38.  
  39. static synchronized def fixButtonClick( aButton ) {
  40. def sKey = 'has_fixButtonClick';
  41. def getValue = {
  42. if ( aButton.getClientProperty( sKey ) == null)
  43. aButton.putClientProperty( sKey, false );
  44. aButton.getClientProperty( sKey )
  45. }
  46. def hasFix = getValue() == true;
  47. if (hasFix)
  48.  
  49. aButton.addMouseListener(
  50.  
  51. void mousePressed(MouseEvent e) {
  52. if ( e.source.hasFocus() == false ) {
  53. e.source.doClick()
  54. }
  55. } // end mousePressed
  56. } // end MouseAdapter
  57.  
  58. );
  59.  
  60. aButton.putClientProperty( sKey, true );
  61. }
  62.  
  63.  
  64. if ( FixButtonClick.initialized == true )
  65.  
  66. FixButtonClick.initialized = true;
  67.  
  68. // Global Event Listener
  69.  
  70. void eventDispatched(AWTEvent event) {
  71.  
  72. def isJButton = event.source instanceof javax.swing.JButton;
  73. def isMouseEvent = event instanceof java.awt.event.MouseEvent;
  74.  
  75. if (isJButton && isMouseEvent) {
  76.  
  77. def MOUSE_EN = java.awt.event.MouseEvent.MOUSE_ENTERED
  78. def isMouseEntered = event.getID() == MOUSE_EN;
  79.  
  80. if ( isMouseEntered ) {
  81.  
  82. def r = new Runnable() {
  83. public void run() {
  84. FixButtonClick.fixButtonClick( event.source );
  85. }
  86. };
  87.  
  88. if ( SU.isEventDispatchThread() ) {
  89. r.run();
  90. }
  91. else {
  92. SU.invokeAndWait( r );
  93. }
  94.  
  95. } // isMouseEntered
  96.  
  97. } // isJButton && isMouseEvent
  98. }
  99. }
  100.  
  101. def tk = Toolkit.getDefaultToolkit();
  102. def long mask
  103. // mask = Long.MAX_VALUE; // all events
  104. mask = AWTEvent.MOUSE_EVENT_MASK;
  105. tk.addAWTEventListener( gel, mask as long );
  106.  
  107. } // end init
  108.  
  109. } // end class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.