JDK-4302322 : AWT Button will fire ActionEvent even if focus is stolen during the click
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.0,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,windows_98,windows_nt
  • CPU: generic,x86
  • Submitted: 2000-01-03
  • Updated: 2002-04-10
  • Resolved: 2002-04-10
Related Reports
Relates :  
Description
AWT Button will fire ActionEvent even if focus is stolen during the click. Originally, this bug was targeted at the Swing JButton (see original report below). Since the filing, JButton has been fixed and now AWT Button has the poor behavior.

Note that this is NOT the same bug as RFE 4533820 which is a Swing RFE to allow InputVerifiers to prevent firing of ActionEvents.
----------------

Name: skT88420			Date: 01/03/2000


java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)


I am working with a JTextField that performs validation when it loses focus. If
an error is encountered, it requests that focus be returned and warns the user.
Under Java AWT this scheme would prevent the user from clicking the Ok button to
leave the form, but it does not work in Swing.

With this problem converting code from AWT to Swing, as I am doing now, can
result in form data being accepted when it is invalid. A number of combinations
were found that would crash the converted program or result in the information
invisibly being discarded.

In the following example, I was never able to get the awt.Button to fire an
action event, but the Swing button would every time.

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

public class Test extends Frame implements ActionListener, FocusListener
{
	public static void main(String[] argv)
	{
		Frame fr = new Test();
		fr.setSize(320, 240);
		fr.show();
	}

	protected TextField text = new TextField(5);
	protected JButton jbtn = new JButton("Swing");
	protected Button btn = new Button("AWT");

	public Test()
	{
		setLayout(new FlowLayout());
	
		text.addFocusListener(this);
		
		jbtn.addActionListener(this);
		btn.addActionListener(this);
	
		add(text);
		add(jbtn);
		add(btn);
	}
	
	public void focusLost(FocusEvent evt)
	{
		System.err.println("Moving focus back to text field");
		text.requestFocus();
	}
	
	public void focusGained(FocusEvent evt)
	{
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource() == btn) {
			System.out.println("AWT button was clicked");
		}
		else if (evt.getSource() == jbtn) {
			System.out.println("Swing button was clicked");
		}
	}
}
(Review ID: 99526) 
======================================================================

Name: apC97674			Date: 01/27/2000


    I can't reproduce it on JDK1.3.0Q - T on WinNT, but it reproduces
on Solaris 2.7 for AWT Button (and not for JButton).

###@###.###

======================================================================

Comments
WORK AROUND Name: skT88420 Date: 01/03/2000 Additional validations check when the user tries to leave the form. ======================================================================
11-06-2004

PUBLIC COMMENTS .
10-06-2004

EVALUATION Works "correctly" for JButton, but not for AWT Button. It may be that we need to test that the button is focused before firing the action. They should be using the input verification API rather than sending focus back in a focusLost handler. hania.gajewska@Eng 2001-04-17 Commit to Tiger (developer demand). ###@###.### 2001-11-01 Committing to hopper. ###@###.### 2001-11-15 JButton has worked as requested for quite some time now. It seems as though AWT Button used to work the same but no longer does so in 1.4. There appears to be some confusion surrounding this particular bugid. It has 74 votes, a number that I beleive is higher than it deserves. I believe I have discovered the reason. The synopsis "JButton will fire ActionEvent even if focus is stolen during the click" is very similar to a problem that we are having in Swing. The Swing problem has to do with InputVerifiers and their lack of interaction with JButtons. There is now an RFE open for the JButton issue (4533820). To prevent further confusion, I am updating the synopsis and the description. ###@###.### 2001-12-03 We have decided not to change the behavior of AWT Buttons. However, the behavior of Swing JButtons may be tracked under 4533820. ###@###.### 2002-04-09
03-12-2001