Bug 25165 - Swing components give up and reclaim focus improperly
Summary: Swing components give up and reclaim focus improperly
Status: RESOLVED FIXED
Alias: None
Product: classpath
Classification: Unclassified
Component: swing (show other bugs)
Version: unspecified
: P3 normal
Target Milestone: 0.20
Assignee: Anthony Balkissoon
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-11-29 21:54 UTC by Anthony Balkissoon
Modified: 2005-11-30 19:30 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
patch (337 bytes, patch)
2005-11-30 18:15 UTC, Anthony Balkissoon
Details | Diff
new patch (589 bytes, patch)
2005-11-30 19:29 UTC, Anthony Balkissoon
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Anthony Balkissoon 2005-11-29 21:54:22 UTC
The simple testcase pasted below shows that Swing components that are already focused are giving up and re-claiming focus when they're clicked on.  Running on the JDK shows that there should be no FOCUS_LOST or FOCUS_GAINED events fired at all.


***TESTCASE***
import java.awt.event.*;
import javax.swing.*;


public class Focus
{ 
  public static void main(String args[])
  {
    JButton b = new JButton();
    b.addFocusListener(new FocusListener()
      {
        public void focusGained(FocusEvent e)
        {
          System.out.println ("g");
        }
        public void focusLost (FocusEvent e)
        {
          System.out.println ("l");
        }
      });
    JFrame f = new JFrame();
    f.setSize(300,300);
    f.add(b);
    f.setVisible(true);
  }
}
Comment 1 Anthony Balkissoon 2005-11-30 18:15:39 UTC
Created attachment 10371 [details]
patch
Comment 2 Anthony Balkissoon 2005-11-30 18:15:56 UTC
Fixed, patch attached.
Comment 3 Anthony Balkissoon 2005-11-30 19:04:52 UTC
The improved test case shown below shows that this bug was fixed incorrectly.  Since Component.processEvent and Component.processFocusEvent can be overridden we must handle this problem BEFORE these methods are invoked, as the test case below shows that they aren't called on the JDK (when you click on the button) but they ARE currently called in our implementation.

***TEST CASE***
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Focus
{ 
  public static void main(String args[])
  {
    JButton b = new JButton()
      {
        protected void processFocusEvent(FocusEvent e)
        {
          System.out.println ("processFocusEventCalled");
          super.processFocusEvent(e);
        }
        protected void processEvent (AWTEvent e)
        {
          if (e instanceof FocusEvent)
            System.out.println ("processEvent called");
          super.processEvent(e);
        }
      };
    b.addFocusListener(new FocusListener()
      {
        public void focusGained(FocusEvent e)
        {
          System.out.println ("\n\ngained");
          Thread.dumpStack();
        }
        public void focusLost (FocusEvent e)
        {
          System.out.println ("\n\nlost");
          Thread.dumpStack();
        }
      });
    JFrame f = new JFrame();
    f.setSize(300,300);
    f.add(b);
    f.setVisible(true);
  }
}
Comment 4 cvs-commit@developer.classpath.org 2005-11-30 19:19:54 UTC
Subject: Bug 25165

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch: 	
Changes by:	Anthony Balkissoon <abalkiss@savannah.gnu.org>	05/11/30 19:19:24

Modified files:
	.              : ChangeLog 
	java/awt       : Component.java 

Log message:
	2005-11-30  Anthony Balkissoon  <abalkiss@redhat.com>
	
	Fixes bug #25165
	* java/awt/Component.java:
	(processFocusEvent): Don't check if focus opposite is the same as the
	receiving Component, this is now done in dispatchEventImpl.
	(dispatchEventImpl): Don't dispatch FocusEvents whose opposite
	Components are the same.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.5753&tr2=1.5754&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/classpath/classpath/java/awt/Component.java.diff?tr1=1.90&tr2=1.91&r1=text&r2=text




Comment 5 Anthony Balkissoon 2005-11-30 19:29:54 UTC
Created attachment 10372 [details]
new patch
Comment 6 Anthony Balkissoon 2005-11-30 19:30:16 UTC
This is fixed properly now.