Subclassing - how to handle button events from base class

Discussion in 'Java' started by Tim, Dec 3, 2003.

  1. Tim

    Tim Guest

    Hi

    My base class has buttons who's events I want to handle in the
    subclass - how should I do this ? (I want to handle the button
    differently in the specialised subclasses)

    Should I add an actionlistener in the subclass ?? Or is it possible to
    overide the actionperformed function registered on the button in the
    base class ? (Sun studio 4 makes this function private)

    Thanks
    Tim
    Tim, Dec 3, 2003
    #1
    1. Advertising

  2. Tim

    Jeffrey Palm Guest

    Tim wrote:

    > Hi
    >
    > My base class has buttons who's events I want to handle in the
    > subclass - how should I do this ? (I want to handle the button
    > differently in the specialised subclasses)
    >
    > Should I add an actionlistener in the subclass ?? Or is it possible to
    > overide the actionperformed function registered on the button in the
    > base class ? (Sun studio 4 makes this function private)
    >
    > Thanks
    > Tim


    I can think of two ways -- both are bad, though, because they call
    virtual (non-final) methods in the constructor. One is to have your
    frame implements ActionListener and then override the actionPerformed
    method in the subclass. True, your aren't *really* calling a virtual
    method in the constructor, but passing 'this' to another method is
    basically equivalent. Second, you could make a virtual method to create
    an Action for a button -- here's a brief example:


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

    public class Tim {

    public static void main(String[] args) {
    Example1.go();
    Example2.go();
    }

    static class Example1 {
    static void go() { show(new SuperFrame()); show(new SubFrame());; }
    static class SuperFrame extends JFrame implements ActionListener {
    SuperFrame(String s) {
    super(s);
    JButton b = new JButton("1");
    getContentPane().add(b);
    b.addActionListener(this);
    }
    SuperFrame() {this("Super1");}
    public void actionPerformed(ActionEvent e) {
    System.out.println("Super:"+e);
    }
    }
    static class SubFrame extends SuperFrame {
    SubFrame() {super("Sub1");}
    public void actionPerformed(ActionEvent e) {
    System.out.println("Sub:"+e);
    }
    }
    }

    static class Example2 {
    static void go() { show(new SuperFrame()); show(new SubFrame());; }
    static class SuperFrame extends JFrame {
    SuperFrame(String s) {
    super(s);
    getContentPane().add(new JButton(createAction()));
    }
    Action createAction() {
    return new AbstractAction("2") {
    public void actionPerformed(ActionEvent e) {
    System.out.println("Super:"+e);
    }
    };
    }
    SuperFrame() {this("Super2");}
    }
    static class SubFrame extends SuperFrame {
    SubFrame() {super("Sub2");}
    Action createAction() {
    return new AbstractAction("2") {
    public void actionPerformed(ActionEvent e) {
    System.out.println("Super:"+e);
    }
    };
    }
    }
    }

    static void show(JFrame f) {
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
    }
    }


    /////////////////////////////////////// End ////
    Jeff

    --
    Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm
    Jeffrey Palm, Dec 3, 2003
    #2
    1. Advertising

  3. (Tim) writes:
    > My base class has buttons who's events I want to handle in the
    > subclass - how should I do this ? (I want to handle the button
    > differently in the specialised subclasses)


    I would do a very conservative design: A method/Methods in the base
    class which allow anyone (not only a subclass) to register event
    listeners for the buttons.

    My second-best choice would be to register event listeners in the base
    class which call empty or abstract handling methods of the base class.
    Subclasses would have to override these methods.

    /Thomas
    Thomas Weidenfeller, Dec 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M Skabialka
    Replies:
    4
    Views:
    829
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    532
    John Carson
    Sep 3, 2005
  3. Hicham Mouline
    Replies:
    1
    Views:
    583
    Victor Bazarov
    Apr 20, 2009
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    340
    Chris
    Aug 30, 2005
  5. Karan Rajput
    Replies:
    2
    Views:
    130
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page