user1625324
user1625324

Reputation: 93

using ActionListener into another ActionListener

I want to use the first ActionListener (About) into the second ActionListener (About2) without copying the first one into teh second one are there any way to do that?

About.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
          AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
       }
       }
   );



About2.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){

                  ////////code here///////////////
       }
       }
   );

Upvotes: 1

Views: 282

Answers (2)

Lews Therin
Lews Therin

Reputation: 10995

The simplest thing to do here would be to copy the reference of that anonymous object into a temp variable and pass that reference.

ActionListener temp= new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
          AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
       }
       };

About.AddActionListener(temp);
About2.AddActionListener(temp);

Another option would be to make your class implement ActionListener and simply do:

About.AddActionListener(this)
About2.AddActionListener(this);

While you can do the above as stated in the comment it isn't the best idea. Another option is create another class to implement ActionListener and create an instance of that class.

public class ReusableListener implements ActionListener

ActionListener listener = new ReusableListener() ;//as a field

About.addActionListener(listener) ;
About2.addActionListener(listener) ;

Upvotes: 2

Austin
Austin

Reputation: 4929

Well as long as you're not using the ActionEvent argument, you could just create a method like this in your class,

public void doAction() {
 AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
}

and in your actionPerformed methods, just call

doAction();

So like this,

About.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
doAction();

   );

Upvotes: 1

Related Questions