Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tauqueer Ali
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
b1.setVisible(false);
fr.getContentPane().remove(b1);
b0.requestFocus();
}
});
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());
}
};
b0.addFocusListener(fl);
b1.addFocusListener(fl);
b2.addFocusListener(fl);

fr.getContentPane().add(b0,"North");
fr.getContentPane().add(b1);
fr.getContentPane().add(b2,"South");
fr.setSize(200,200);
fr.setVisible(true);
}
}
 
Sonny Pondrom
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it have anything to do with it being "final"?
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could wrap your requestFocus as shown below:

You should probably use requestFocusInWindow().
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On My System, Bo is getting focus. I am using JDK 1.4.1
 
Stuart Friedberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
reliable.
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic