JDK-4707457 : focusGained and focusLost methods under FcousListener in 1.4 do not work as 1.3
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0_01
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2002-06-25
  • Updated: 2022-09-09
  • Resolved: 2003-12-18
Related Reports
Duplicate :  
Relates :  
Description
focusGained and focusLost methods under FocusListener in SDK 1.4.0_01 do not wor
k the same way as the older version.  


Customer's application used to work fine under SDK 1.3.1.  However, focusGained
and focusLost methods do not work in SDK 1.4.0_01.      



The test case is as follows.  See the differences between 1.3.1 and 1.4


TestActionListener.java
=========================


/*
 * TestActionListener.java
 *
 * Created on June 21, 2002, 4:07 PM
 */

/**
 *   Case # 63052326
 *   author  Philip Chou  310-764-6895
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestActionListener extends JFrame
{
    
    /** Creates a new instance of TestActionListener */
    public TestActionListener() 
    {
        this.getContentPane().setLayout(new BorderLayout());
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        
        JPanel topPane = new JPanel();
        topPane.setLayout(layout);
        
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(1);
            }
        });
        
        SuperFocusListener zz = new SuperFocusListener();
        // Menu Bar
        JMenuBar menuBar = new JMenuBar();
        
        JMenu menu = new JMenu("Tester");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription("demo for Tester");
        menu.addFocusListener(zz);
        menuBar.add(menu);

        //a group of JMenuItems
        JMenuItem menuItem = new JMenuItem("Click", KeyEvent.VK_T);
        menuItem.addFocusListener(zz);
        
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
        
        menuItem.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent e) 
            {
                System.out.println("Action.");
            }
        });
        menu.add(menuItem);
        
        //a group of radio button menu items
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();
        JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        rbMenuItem.setMnemonic(KeyEvent.VK_R);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);

        rbMenuItem = new JRadioButtonMenuItem("Another one");
        rbMenuItem.setMnemonic(KeyEvent.VK_O);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        
        //Build second menu in the menu bar.
        menu = new JMenu("Another");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        menuBar.add(menu);
        
        c.gridx = 0; c.gridy = 1;
        c.weightx = 1.0; c.weighty = 0.0;
        layout.setConstraints(menuBar, c);
        
        topPane.add(menuBar);
        
        Container contentPane = this.getContentPane();
        JTextArea output = new JTextArea(5, 30);
        output.setText("Tester, Please click between 'Tester' and 'Another'.\nYou should see \"FocusGained\" and \"FocusLost\" displayed on the screen. \nYet you won't see them under JDK 1.4.0_01.");
        output.setEditable(true);
        
        JScrollPane scrollPane = new JScrollPane(output);
        
        contentPane.add(BorderLayout.NORTH, topPane);
        
        contentPane.add(BorderLayout.SOUTH, scrollPane);
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        TestActionListener xx = new TestActionListener();
        xx.setLocation(new Point(200, 200));
        xx.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        xx.setSize(450, 500);
        xx.setVisible(true);        
        xx.pack();
        xx.show();
    }
    
}

SuperFocusListener.java
==================

import java.awt.*;
import java.awt.event.*;

    public class SuperFocusListener implements FocusListener 
    {
        public void onShown (SuperFocusListener act) 
        {
        }
        
        //======================================================================
        // Implementing FocusListener
        //======================================================================
        public void focusLost (FocusEvent e) 
        { 
            System.out.println("FocusLost");
        }

        public void focusGained (FocusEvent e) 
        {
            System.out.println("FocusGained");
            onShown(this);
        }
   
    }











Comments
EVALUATION Name: osR10079 Date: 07/01/2002 in the testcase user registers focus listener on JMenu. In 1.3 JMenu receives focus events, in 1.4 doesn't. But AWT does nothing special to dispatch focus events on JMenu. From AWT's point of view this is ordinary component and it will receive focus events when it starts or stops being a focus owner. I suspect that swing implementation was changed somehow and now JMenu doesn't take the focus. I'm not sure that this is a bug, listening focus events on JMenu is strange for me. But if even this is a bug, this is not AWT bug, thus i reassign it to swing for further investigation. ###@###.### July 01, 2002 ====================================================================== Name: azR10139 Date: 12/18/2003 Menu elements do not receive focus since release 1.4. Thus is not a bug, because the focusLost and focusGained events never meant to be used to determine the status of the menus and menu items. Please, use PopupMenuListener to watch for the popup state and ItemListener on the JMenuItem's model to watch for the menuItem is being selected/deselected. ###@###.### 12/18/2003 ======================================================================
18-12-2003