File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Is it a jdk1.4  focus problem,  please help !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is it a jdk1.4  focus problem,  please help !!" Watch "Is it a jdk1.4  focus problem,  please help !!" New topic

Is it a jdk1.4 focus problem, please help !!

Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
Below is my sample program. There are 3 buttons b0, b1, b2. When I press b1, I hide b1 and remove b1 and requestFocus for b0. But what happens is b2 gets focus and only when I move the mouse over the frame b0 gets focus.
import javax.swing.*;
import java.awt.event.*;
public class FocusLost
public static void main(String s[])
final JFrame fr = new JFrame("FoucsLost");
final JButton b0 = new JButton("Zero");
final JButton b1 = new JButton("One");
JButton b2 = new JButton("Two");
b1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
FocusListener fl = new FocusListener()
public void focusGained(FocusEvent e)
System.out.println("focusGained : " + ((JButton)e.getSource()).getText());
public void focusLost(FocusEvent e)
System.out.println("focusLost : " + ((JButton)e.getSource()).getText());


SCJP2, SCWCD, XML, OOAD<br />Kolkata, India
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
Would it have anything to do with it being "final"?
Tim Troy
Ranch Hand

Joined: Apr 02, 2003
Posts: 51
You could wrap your requestFocus as shown below:

You should probably use requestFocusInWindow().
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
On My System, Bo is getting focus. I am using JDK 1.4.1
Stuart Friedberg

Joined: Aug 11, 2003
Posts: 8
This is a feature, perhaps not completely thought out,
rather than a bug.
Java 1.4 changed quite a few details of focus cycle handling. The
changes are pretty well documented if you start with the overview
in the on-line docs: api/java/awt/doc-files/FocusSpec.html
What you've run into is automatic focus transfer _from_ a component
that's being disabled or hidden. This is an intentional feature,
mentioned toward the end of the Programmatic Traversal section of
the FocusSpec documentation. But as you're found out, there is a
delay between deciding to do a transfer and actually doing it.
This can conflict with your own manual transfers that take place
during the delay. You can move the focus _before_ disabling the
component, or you can attempt to delay your transfer until after
the already delayed automatic transfer. The first option is more
In addition to the problem you've encountered, there are some "duh"
bugs related to automatic focus transfer. See BugID 4685768 in the
Java Bug Parade, for example. The main purpose of automatic focus
transfer is to make sure focus never gets stuck on an disabled
component, thus improving keyboard navigability (and accessibility).
Yet automatic focus transfer can _send_ focus to a disabled component.
I agree. Here's the link:
subject: Is it a jdk1.4 focus problem, please help !!
It's not a secret anymore!