Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8048109

JToggleButton does not fire actionPerformed under certain conditions

XMLWordPrintable

    • b05
    • x86_64
    • windows_7, windows_10

      FULL PRODUCT VERSION :
      java version "1.7.0_17"
      Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      I have a JToggleButton that will post/take down a JPopupMenu when the button is selected. If the button is selected and the menu is not posted the action listener will post the menu. If the button is selected and the menu is displayed the action listener will take the menu down. The use case is:
      1 - select button
      2 - menu posted
      3 - select button
      4 - menu taken down

      When I have the Metal look and feel the use case works fine. When I have the Windows look and feel the second button selection does not fire the actionPerformed event. I need to select the button a third time. The Windows scenario is:
      1 - select button
      2 - menu posted
      3 - select button (action event not fired)
      4 - select button
      5 - menu taken down

      The issue is the button must be selected twice after the menu is posted to have the actionPerformed event to fire when using the Windows look and feel.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample code using Metal then Windows look and feel. The user interactions are:

      Metal look and feel
      1 - select the button...the menu will post
      2 - select the button again...the menu will come down

      Windows look and feel
      1 - select the button...the menu will post
      2 - select the button again...the menu stays up...action listened not called
      3 - select the button again...the menu comes down

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected the Windows look and feel to act like the Metal look and feel.
      ACTUAL -
      Windows look and feel
      1 - select the button...the menu will post
      2 - select the button again...the menu stays up...action listened not called
      3 - select the button again...the menu comes down

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.towers.buttonwithpopup;

      import java.awt.Container;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JFrame;
      import javax.swing.JMenuItem;


      public class StartSnippet {

      private static MyPopupMenu popup = null;
      private static MyButton jtb = null;

      /**
      * @param args
      */
      public static void main(String[] args) {
      // When running on Windows systemLaf will be Windows. The commented out code will set the look and feel to Windows instead of Metal

      // String systemLaf = UIManager.getSystemLookAndFeelClassName();
      // System.out.println("System Look and Feel: " + systemLaf);
      // if( systemLaf != null )
      // {
      // try
      // {
      // UIManager.setLookAndFeel( systemLaf );
      // }
      // catch( Exception ex )
      // {
      //
      //
      // }
      // }

      jtb = new MyButton("Press Me");

      jtb.addActionListener(new ActionListener( ) {
      @Override
      public void actionPerformed(ActionEvent ev) {
      System.out.println("ActionEvent!");
      if (!popup.isVisible()) {
      postUp();
      }
      else
      {
      postDown();
      }
      }
      });

      JFrame f = new JFrame( );
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(300, 400);
      Container c = f.getContentPane( );
      c.setLayout(new FlowLayout( ));
      c.add(jtb);
      f.setVisible(true);
      popup = new MyPopupMenu();
      popup.add(new JMenuItem("A"));
      popup.add(new JMenuItem("B"));
      popup.add(new JMenuItem("C"));
      popup.setVisible(false);
      }
      public static void postUp() {
      popup.setInvoker(jtb);
      popup.setVisible(true);
      }
      public static void postDown() {
      popup.setVisible(false);
      }


      }

      package com.towers.buttonwithpopup;

      import java.awt.event.MouseEvent;

      import javax.swing.JButton;

      public class MyButton extends JButton {

      /**
      *
      */
      private static final long serialVersionUID = 1L;

      public MyButton(String string) {
      // TODO Auto-generated constructor stub
      super ( string);
      }

      @Override
      protected void processMouseEvent(MouseEvent e) {
      System.out.println("processMouseEvent" + e.getID());
      super.processMouseEvent(e);
      }

      }

      package com.towers.viewjpopup.snippet.views;

      import java.io.PrintWriter;
      import java.io.StringWriter;

      import javax.swing.JPopupMenu;

      public class MyPopupMenu extends JPopupMenu {
      /**
      *
      */
      private static final long serialVersionUID = 1L;



      @Override
      public void setVisible( boolean state )
      {
      if( !state )
      {
      Exception ex = new Exception();
      StringWriter stringWriter = new StringWriter();
      PrintWriter printWriter = new PrintWriter( stringWriter );
      ex.printStackTrace( printWriter );
      String traceString = stringWriter.getBuffer().toString();
      if( traceString.lastIndexOf( "windowDeactivated" ) > 0
      || traceString.lastIndexOf( "menuSelectionChanged" ) > 0 )
      {
      return;
      }
      }
      super.setVisible(state);

      }
      }
       

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The work around is to select the JToggleButton 2x to bring down the Popup menu.

            trebari Tejpal Rebari (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: