actions on JPanel objects

Discussion in 'Java' started by mwave3k, Oct 5, 2006.

  1. mwave3k

    mwave3k Guest

    I have a main project GUI with tabs on it. these tabs have JPanels
    with various JComponents on them. The JPanels are all made in seperate
    classes that extend JPanel. the point of that is to make my code a
    little cleaner. All the components in JPanel are private. However, I
    don't know how to add an ActionListener to buttons and other components
    on the JPanel and recieve these actions in the main project rather then
    the JPanel class. Help please? i would appreciate it
     
    mwave3k, Oct 5, 2006
    #1
    1. Advertising

  2. mwave3k

    Ian Wilson Guest

    mwave3k wrote:
    > I have a main project GUI with tabs on it. these tabs have JPanels
    > with various JComponents on them. The JPanels are all made in seperate
    > classes that extend JPanel. the point of that is to make my code a
    > little cleaner. All the components in JPanel are private.


    Seems familar.

    > However, I
    > don't know how to add an ActionListener to buttons and other components
    > on the JPanel and recieve these actions in the main project rather then
    > the JPanel class.


    Me neither, so what I do instead is, have each panel implement
    ActionListener and pass the panel a reference to the JFrame in it's
    constructor, then the actionListener can invoke a method in the JFrame
    to handle the event.

    e.g. something not entirely unlike this

    JPanel Foo extends JPanel implements ActionListener {
    JFrame parent;
    Foo(JFrame parent) {
    this.parent = parent;
    ...
    fooButton.addActionListener(this);
    ...
    }
    public void actionPerformed(ActionEvent e) {
    parent.doCommand(e.getActionCommand());
    }
    }

    untested, caveat emptor.
     
    Ian Wilson, Oct 5, 2006
    #2
    1. Advertising

  3. mwave3k

    Ian Wilson Guest

    Ian Wilson wrote:
    > mwave3k wrote:
    >
    >> I have a main project GUI with tabs on it. these tabs have JPanels
    >> with various JComponents on them. The JPanels are all made in seperate
    >> classes that extend JPanel. the point of that is to make my code a
    >> little cleaner. All the components in JPanel are private.

    >
    >
    > Seems familar.
    >
    >> However, I
    >> don't know how to add an ActionListener to buttons and other components
    >> on the JPanel and recieve these actions in the main project rather then
    >> the JPanel class.

    >
    >
    > Me neither, so what I do instead is, have each panel implement
    > ActionListener and pass the panel a reference to the JFrame in it's
    > constructor, then the actionListener can invoke a method in the JFrame
    > to handle the event.
    >
    > e.g. something not entirely unlike this
    >
    > JPanel Foo extends JPanel implements ActionListener {
    > JFrame parent;
    > Foo(JFrame parent) {
    > this.parent = parent;
    > ...
    > fooButton.addActionListener(this);
    > ...
    > }
    > public void actionPerformed(ActionEvent e) {
    > parent.doCommand(e.getActionCommand());
    > }
    > }
    >


    It occurrs to me you could just do
    fooButton.addActionListener(parent);
    and add some comment's in the parent's actionPerformed() to clarify that
    it receives events from random JPanels.
     
    Ian Wilson, Oct 5, 2006
    #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. steve smith
    Replies:
    0
    Views:
    558
    steve smith
    Jul 10, 2003
  2. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,155
  3. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,624
    John B. Matthews
    Jul 18, 2008
  4. t1m1976
    Replies:
    1
    Views:
    2,519
    t1m1976
    Nov 7, 2010
  5. speedbird
    Replies:
    0
    Views:
    119
    speedbird
    Mar 24, 2008
Loading...

Share This Page