Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: passing arguments in ActionListener class

  1. #1
    sandeshyadav is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

    Default passing arguments in ActionListener class

    i have created several JFrames with similar functionality .. i want to write a single ActionHandler class for three JFrames with similar functionality ... how can i pass JFrames as arguments to the actionPerformed() method ... is it possible ? please help ......

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: passing arguments in ActionListener class

    It's possible, either directly or indirectly. For a direct approach, give your ActionHandler class a JFrame field, one that is filled via a constructor parameter:

    Java Code:
    class MyHandler implements ActionListener {
      JFrame myFrame;
    
      public MyHandler(JFrame myFrame) {
        this.myFrame = myFrame;
        // and whatever else needs to go in here
      }
    
      public void actionPerformed(ActionEvent evt) {
        myFrame.someMethod
      }
    }

    The indirect approach would let you get the reference from the source held by ActionEvent
    Java Code:
    class MyHandler implements ActionListener {
      
      public MyHandler(JFrame myFrame) {
        // and whatever is needed to go in here
      }
    
      public void actionPerformed(ActionEvent evt) {
        Component component = (Component) evt.getSource();
        Window win = SwingUtilities.getWindowAncestor(component);
      }
    }
    Note that the Window returned should be the top level window that holds the JButton or JMenuItem. Then you'd cast it to whatever it truly is and use it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: passing arguments in ActionListener class

    A single application with 3 JFrames sounds like a bad design. For most use cases, an application would have just one JFrame. Other parts of the UI that are not to be shown on startup may be implemented as modal JDiallogs (or via the static methods of JOptionPane), or as cards in a CardLayout. Or, for a MDI application, as JInternalFrames in a JDesktopPane.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    sandeshyadav is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

    Default Re: passing arguments in ActionListener class

    Quote Originally Posted by Fubarable View Post
    It's possible, either directly or indirectly. For a direct approach, give your ActionHandler class a JFrame field, one that is filled via a constructor parameter:

    Java Code:
    class MyHandler implements ActionListener {
      JFrame myFrame;
    
      public MyHandler(JFrame myFrame) {
        this.myFrame = myFrame;
        // and whatever else needs to go in here
      }
    
      public void actionPerformed(ActionEvent evt) {
        myFrame.someMethod
      }
    }

    The indirect approach would let you get the reference from the source held by ActionEvent
    Java Code:
    class MyHandler implements ActionListener {
      
      public MyHandler(JFrame myFrame) {
        // and whatever is needed to go in here
      }
    
      public void actionPerformed(ActionEvent evt) {
        Component component = (Component) evt.getSource();
        Window win = SwingUtilities.getWindowAncestor(component);
      }
    }
    Note that the Window returned should be the top level window that holds the JButton or JMenuItem. Then you'd cast it to whatever it truly is and use it.

    Thanx a lot ...... for getting me out of my confusion ...........

  5. #5
    sandeshyadav is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

    Default Re: passing arguments in ActionListener class

    Quote Originally Posted by DarrylBurke View Post
    A single application with 3 JFrames sounds like a bad design. For most use cases, an application would have just one JFrame. Other parts of the UI that are not to be shown on startup may be implemented as modal JDiallogs (or via the static methods of JOptionPane), or as cards in a CardLayout. Or, for a MDI application, as JInternalFrames in a JDesktopPane.

    db

    Thanx .... i got your point and i am learnig to use JInternalFrames now .... Thanx again.

Similar Threads

  1. Passing arguments from java to python
    By wavefunction in forum Advanced Java
    Replies: 4
    Last Post: 07-10-2011, 05:36 PM
  2. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 12:01 AM
  3. Passing arguments to jar file with a pipe
    By clover in forum New To Java
    Replies: 7
    Last Post: 06-27-2011, 05:36 PM
  4. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 04:00 PM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •