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).
###@###.###
======================================================================