-
Bug
-
Resolution: Fixed
-
P3
-
1.1.4, 1.3.0
-
beta
-
x86, sparc
-
solaris_2.5, windows_nt
Name: mt13159 Date: 09/11/2000
Setting focus during window activation often sends focus to the wrong place, or to multiple places. A simple test case follows. It puts up four windows in a row. Each window contains one lightweight component. These simple components just draw a crosshair when they have focus, or an empty circle when they don't have focus. When a window is activated, it calls requestFocus() on its component. The expected behavior would be for the last window to be active and have focus.
When you run this test case in different versions of Java, however, you get various incorrect results. In the 1.1 JDK's, multiple components end up with focus at once. In 1.3, a window other than the active one ends up with focus. Results can vary from run to run.
import java.awt.*;
import java.awt.event.*;
public class ActivateFocus extends Frame {
LightWeight lw = null;
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
ActivateFocus af = new ActivateFocus(i);
af.setLocation(i * 160, 0);
af.setVisible(true);
}
}
public ActivateFocus(int i)
{
setTitle("Window " + i);
lw = new LightWeight(i);
addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e)
{
if (lw != null)
{
lw.requestFocus();
}
}
});
add(lw);
pack();
}
// A very simple lightweight component
class LightWeight extends Component implements FocusListener {
boolean focused = false;
int num;
public LightWeight(int num) {
this.num = num;
addFocusListener(this);
}
public void paint(Graphics g) {
Dimension size = getSize();
int w = size.width;
int h = size.height;
g.setColor(getBackground());
g.fillRect(0, 0, w, h);
g.setColor(Color.black);
g.drawOval(0, 0, w-1, h-1);
if (focused) {
g.drawLine(w/2, 0, w/2, h);
g.drawLine(0, h/2, w, h/2);
}
}
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
public void focusGained(FocusEvent e) {
focused = true;
if (e.isTemporary()) {
System.out.print("Temporary ");
}
System.out.println("focusGained on " + e.getComponent());
repaint();
}
public void focusLost(FocusEvent e) {
focused = false;
if (e.isTemporary()) {
System.out.print("Temporary ");
}
System.out.println("focusLost on " + e.getComponent());
repaint();
}
public String toString() {
return ("Component " + num);
}
}
}
(Review ID: 109352)
======================================================================
- duplicates
-
JDK-4070691 Solaris and win32 differ in the way windowActivated() gets called (focus issue)
-
- Closed
-