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);
}
}