简体   繁体   中英

Define and ActionListener in class which extends Button

How to place Action Listener directly in definition of class that extends Button ?

If object of class Button is created then we could simply use anonumous inner class :

b = new Button("Click me");
b.addActionListener(
                    new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            System.out.println("stringToPrint");
                        }
                    }
                );

how to do the same in below :
class CustomizedButton extends Button{
   String customClass;

   Button(String stringToPrint){
      super(customClass); //customClass is also button name
      this customString = stringToPrint;
   }

   /*this.addActionListener( //don't work this way
       new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.out.println(customClass);//use outer(?) field
            }
        }
     );*/
}

I need to create 20 almost identical but slightly different buttons, so anonymous inner is too long

You could declare a private nested class, like so:

public class CustomizedButton extends Button{
    String customClass;

    CustomizedButton(String stringToPrint){
        super(customClass); //customClass is also button name
        this.customString = stringToPrint;
        addActionListener(new MyListener());
    }

    private class MyListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO: listener code here
        }
    }
}

But it's not much different from using an anonymous inner class or lambda:

public class CustomizedButton extends Button{
    String customClass;

    CustomizedButton(String stringToPrint){
        super(customClass); //customClass is also button name
        this.customString = stringToPrint;
        addActionListener(e -> myListenerCode(e));
    }

    private void myListenerCode(ActionEvent e) {
        // TODO: listener code here
    }

}

Having said this, other issues come to mind:

  • Usually it's best to favor composition over inheritance. I would bet that what you really want is some sort of factory method that creates your button complete with listener
  • Why use AWT components such as the java.awt.Button class when it is 20+ yrs out of date? Why not Swing JButtons instead?
  • If you were using Swing JButtons, best would be to create a custom Action rather than extend JButton. Actions can hold and change many button properties, including a listener, the displayed text, icons, the tool tip text (displayed on hover)....
  • For that matter, you should favor JavaFX if this is a new project, since this is the current best-supported Java GUI library.

For example an AbstractAction class could look something like:

public class CustomizedAction extends AbstractAction{
    String text;

    CustomizedAction(String text, int mnemonic){
        super(text); //text is also button name
        this.text = text;
        putValue(MNEMONIC_KEY, mnemonic); // for alt-key short cut if desired
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String currentName = getValue(NAME); // same value as the text field
        System.out.println(currentName);

        // TODO: more listener code here
    }

}

and could be used like so:

JButton button = new JButton(new CustomizedAction("Foo", KeyEvent.VK_F));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM