Name: rpR10076 Date: 03/07/2002
###@###.###
Sometimes, if a lightweight component within heavyweight
container receives focus, focus requests to other lightweight
components within the same heavyweight are delayed till next
event arrives.
The problem is demonstrated by included test case, FocusDelay.java.
Start the program, there will be a window with two JButtons and
one JTextField. Click on the JButton titled "Click me!". Its action
listener calls requestFocus() on another JButton and then on the
JTextField. If you don't move the mouse after the click, the focus
will be on JButton titled "Do I have focus?". If you move the mouse,
the focus will move to the JTextField. However, the focus should
move to JTextField without delay.
=================================================================
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class FocusDelay {
JFrame testFrame;
JButton testButton1;
JButton testButton2;
JTextField testField;
public FocusDelay() {
testFrame = new JFrame("See my components!");
testFrame.setFocusable(true);
testButton1 = new JButton("Click me!");
testButton2 = new JButton("Do I have focus?");
testField = new JTextField("Do I have focus?");
testButton1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
testButton2.requestFocus();
testField.requestFocus();
}
}
);
testFrame.getContentPane().setLayout(new FlowLayout());
testFrame.getContentPane().add(testButton1);
testFrame.getContentPane().add(testField);
testFrame.getContentPane().add(testButton2);
}
public static void main(String[] args) {
FocusDelay fd = new FocusDelay();
fd.testFrame.setSize(300, 100);
fd.testFrame.setVisible(true);
}
}
==============================================================
======================================================================