Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4301264

ActionPerformed Event Fails to Fire for JButton

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      02
    • CPU:
      x86
    • OS:
      windows_nt
    • Verification:
      Verified

      Backports

        Description

        When three JButtons are clicked quickly, and the actionPerformed method for each JButton performs a lot of work, the actionPerformed method frequently
        fails to execute for one of the JButtons, and mouse coordinates are reported
        incorrectly. This occurs with 1.2.2 and 1.3 on NT 4.0 SP 4.

        The test procedure: Press and release on button A, press and release on
        button B, press and release on button C, and move the mouse so it's not over
        any button. You'll see a printout for every mouse-pressed, mouse-released,
        and action event. In addition, every action event causes a pause of 2
        seconds while we do some work to busy the VM.

        You'd except to see action events from A, then B, then C. But with 1.2.2-004, sometimes you'll see just A then B, or sometimes A, then C, then B! Often the mouseis reported to be not on the button. If you perform the same test of the AWT buttons (D E F) instead of the JButtons, it always works fine.

        With 1.3, ususally only the action event for A is displayed.

        The problem also occurs with the Solaris reference 1.2.2 JDK, although the
        mouse location reporting is more accurate.

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

        public class JButtonTest implements ActionListener, MouseListener
        {
        public final static void main (String[] args)
        {
        try
        {
        JButtonTest test = new JButtonTest();

        JFrame f = new JFrame("hi");
        f.getContentPane().setLayout(null);
        f.setBounds(100,100,400,300);

        f.getContentPane().addMouseListener(test);

        // Create a bunch of buttons.

        JButton b = new JButton();
        b.addActionListener(test);
        b.addMouseListener(test);
        b.setBounds(50,50,80,30);
        b.setText("A");
        f.getContentPane().add(b);

        b = new JButton();
        b.addActionListener(test);
        b.addMouseListener(test);
        b.setBounds(150,50,80,30);
        b.setText("B");
        f.getContentPane().add(b);

        b = new JButton();
        b.addActionListener(test);
        b.addMouseListener(test);
        b.setBounds(250,50,80,30);
        b.setText("C");
        f.getContentPane().add(b);

        Button b2 = new Button();
        b2.addActionListener(test);
        b2.addMouseListener(test);
        b2.setBounds(50,150,80,30);
        b2.setLabel("D");
        f.getContentPane().add(b2);

        b2 = new Button();
        b2.addActionListener(test);
        b2.addMouseListener(test);
        b2.setBounds(150,150,80,30);
        b2.setLabel("E");
        f.getContentPane().add(b2);

        b2 = new Button();
        b2.addActionListener(test);
        b2.addMouseListener(test);
        b2.setBounds(250,150,80,30);
        b2.setLabel("F");
        f.getContentPane().add(b2);

        f.show();
        }
        catch(Exception ex)
        {
        ex.printStackTrace();
        }
        }

        public void actionPerformed(ActionEvent e)
        {
        // Print out some stuff.
        Component src = (Component)e.getSource();
        System.out.println("*** Action event on " + getButtonTitle(src) + " ***");

        // Do some busy work for 2 seconds.
        String s = "";
        long time = System.currentTimeMillis();
        long time2;
        do
        {
        time2 = System.currentTimeMillis();
        s += Long.toString(time2);
        }
        while(time2 - time < 2000);
        }

        public void mousePressed(MouseEvent e)
        {
        printout("Mouse pressed", e);
        }
        public void mouseReleased(MouseEvent e)
        {
        printout("Mouse released", e);
        }
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

        private static void printout(String eventType, MouseEvent e)
        {
        // Figure out if the mouse coordinates are really on the source component.
        Component src = (Component)e.getSource();
        Rectangle bounds = src.getBounds();
        bounds.x = bounds.y = 0;
        boolean isReallyOnComponent = bounds.contains(e.getX(), e.getY());

        // Print out some stuff.
        System.out.println(" " + eventType + " on " + getButtonTitle(src)
        + " is really on? = " + isReallyOnComponent
        + " x = " + e.getX()
        + " y = " + e.getY());
        }

        private static String getButtonTitle(Component c)
        {
        if (c instanceof Button)
        return ((Button)c).getLabel();
        if (c instanceof JButton)
        return ((JButton)c).getText();
        return null;
        }
        }



        RESULTS:

        java version "1.2.2"
        HotSpot VM (1.0.1, mixed mode, build g)
        java full version "JDK-1.2.2-003"

               Mouse pressed on A is really on? = true x = 46 y = 15
        *** Action event on A ***
               Mouse released on A is really on? = true x = 46 y = 15
               Mouse pressed on null is really on? = true x = 172 y = 66
               Mouse released on null is really on? = true x = 172 y = 65
               Mouse pressed on B is really on? = false x = 131 y = 21
        *** Action event on B ***
               Mouse released on B is really on? = false x = 131 y = 21

               Mouse pressed on A is really on? = true x = 48 y = 15
        *** Action event on A ***
               Mouse released on A is really on? = true x = 48 y = 15
               Mouse pressed on C is really on? = false x = -62 y = 15
        *** Action event on C ***
               Mouse released on C is really on? = false x = -62 y = 15
               Mouse pressed on B is really on? = false x = 136 y = 12
        *** Action event on B ***
               Mouse released on B is really on? = false x = 136 y = 12

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                dmikhalksunw Denis Mikhalkin (Inactive)
                Reporter:
                nrodinsunw Nick Rodin (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: