Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2003, 11:44 AM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to HalfSkin
Default Better way about actionPerformed?

Which is the better way or any other advice?
I know a fifth way with actionPerformed in an anonymous class, but I
don't like it in this app.

MyAppFrame.java is the GUI implementation of MyApp.java.
A and B are two items in a menu and two buttons in the toolbar:

public class MyAppFrame extends JFrame {

    //Constructors

    public MyAppFrame(String title) {

        setTitle(title);
        ... // add A, B to menu and to toolbar


/* First Way (like on the book)

    A = new MenuItemAction(...);
    B = new MenuItemAction(...);

    // the class sets keystroke, icon, etc and performs actions

        class MenuItemAction extends AbstractAction {

            //Constructors

            MenuItemAction(...) {
        ...
            }

                    public void actionPerformed(ActionEvent e) {

                String name = (String)getValue(NAME);

                if (name.equals(A.getValue(NAME))) {
                // do something ...
                } else if (name.equals(B.getValue(NAME))) {
                    // do something else ...
                }
            }
     }

// First Way end */


/* Second Way

    A = new AActionClass(...);
    B = new BActionClass(...);

        abstract class MenuItemAction extends AbstractAction {

            //Constructors

            MenuItemAction(...) {
        ...
            }
        }

    public class AActionClass extends MenuItemAction {
            //Constructors
            public AActionClass(...) {
                super(...);
            }

            public void actionPerformed(ActionEvent e) {
                // do something ...
            }
     }

    public class BActionClass extends MenuItemAction {
            //Constructors
            public BActionClass(...) {
                super(...);
            }

            public void actionPerformed(ActionEvent e) {
                // do something else ...
            }
     }

// Second Way end */


/* Third Way

// like Second Way with AActionClass and BActionClass implemented in
// separate files (i.e. AActionClass.java and BActionClass.java). But
// I have to change some local vars from private to global, because
// these classes use them in their actionPerformed method.

// Third Way end */


/* Fourth Way

    A = new AActionClass(...);
    B = new BActionClass(...);

    class AActionClass extends AbstractAction {
        //Constructors
        public AActionClass(...) {
            super(...);
        }

        public void actionPerformed(ActionEvent e) {
            // do something ...
        }
     }

    class BActionClass extends AbstractAction {
        //Constructors
        public BActionClass(...) {
            super(...);
        }

        public void actionPerformed(ActionEvent e) {
            // do something else ...
        }
     }

// Fourth Way end */


// local vars

private ...

}

Thanks in advance,
Regargs,
Marcello.










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.