classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] FYI: actioncommand fixes


From: Robert Schuster
Subject: Re: [cp-patches] FYI: actioncommand fixes
Date: Fri, 15 Jul 2005 00:51:13 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.7.8) Gecko/20050514

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi
> 
> This particular piece breaks one of my apps here.
Yes. It needs my other patch for AbstractButton to work correct in all
places.

> The problem with this
> new approach is that getActionCommand() now does return the correct
> value but in javax.swing.DefaultButtonModel.setPressed() an ActionEvent
> gets fired with only the actionCommand set (which may be null) without
> asking the label. Any idea about that?
Yes this is the intended behavior.

To elaborate this a bit:
Create a JButton and its model like this: new JButton("foo").

Asking the JButton for its actioncommand will return "foo" (no real ac
set -> returns the label instead).

However if you retrieve the model and ask it instead it will return
null. The same goes with actionevents: If an actionevent is created from
within the buttonmodel then it will show null as its actioncommand.

Now I our buttons register themselves as ActionListener of their model.
A few days ago this ActionListener's actionPerformed implementation
simply changed the source of the ActionEvent from the model to the
button and dispatched the actionevent to the button's own
actionlisteners. This was troublesome because the ActionEvent's
actionCommand was set by the model (usually to null) and could not be
changed (there is no setter and the class' field is final). I found out
that the JDK does not work this way* and my fix was to simply create a
new ActionEvent for the button's actionlisteners which uses most of the
ActionEvent provided by the model.

I hope this makes sense.

cu
Robert

*Try this on the AWT and Classpath (without my recent patches):
import java.awt.event.*;
import javax.swing.*;

public class Test3 {

        private ActionEvent modelActionEvent;
        private ActionEvent buttonActionEvent;

        public static void main(String[] args)
        {
                new Test3();
        }

        private Test3()
        {
                JButton jb = new JButton("bla");

                ButtonModel bm = jb.getModel();

                bm.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae)
                        {
                                
System.out.println("buttonModel.actionPerformed()");
                                modelActionEvent = ae;
                                System.out.println("event: " + ae);
                        }
                });

                jb.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae)
                        {
                                System.out.println("jbutton.actionPerformed()");
                                buttonActionEvent = ae;
                                System.out.println("event: " + ae);
                        }
                });

                jb.doClick();

                System.out.println("events equal: " + (buttonActionEvent ==
modelActionEvent));
        }

}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFC1uxhG9cfwmwwEtoRAnwHAJ9wy+mv0IKPQ2WkTamz7tRArMPMggCcCF8F
HeJ+mXlkLpa/+uaHXA6TlSw=
=K+tQ
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]