Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

You can implement event listeners in multiple diferent ways. This source code explores some of them.
Check the "Handle events" section under the Events page for an intro to this example.

In this example we use JButton actionListeners but you can use any other type of listners in the same way.

Download source code


Alternative 1 (file: GuiEvents1.java):

Create an anonymous inner class that implements ActionListener and routes the event to a method in your class;
public class GuiEvents1 extends JFrame {
...    
    private void init() {
        ...
        this.button1 = new JButton("generate new number");
        this.button2 = new JButton("reset lable");
        
        //add a handler to the button1 action event:            
        this.button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myButton1Handler(e); //call our handler
            }
        });

        //add a handler to the button2 action event:
        this.button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myButton2Handler(e);
            }
        });
        ...
    }
    
    //method responsible for handling button1 action events: 
    private void myButton1Handler(ActionEvent e) {
        //any logic here
        this.updateLabel();
    }

    //method responsible for handling button2 action events:
    private void myButton2Handler(ActionEvent e) {
        //any logic here
        this.resetLabel();
    }
} 


Alternative 2 (file: GuiEvents2.java)

Make your class implement ActionListener;
public class GuiEvents2 extends JFrame implements ActionListener {
    ...
    private void init() {        
        this.button1 = new JButton("generate new number");
        this.button2 = new JButton("reset lable");
        
        //add a handler to the button1 action event:
        this.button1.addActionListener(this);
        this.button2.addActionListener(this);
        ...
    }
    
    //implementation of the ActionListener Interface:
    @Override
    public void actionPerformed(ActionEvent e) {
        String source = e.getActionCommand();
        out("Action command: " + source);

        if (source.equals(this.button1.getActionCommand())) {
            this.myButton1Handler(e); //a normal class method
        } else if (source.equals(this.button2.getActionCommand())) {
            this.myButton2Handler(e); //a normal class method
        }
    }
    ...
} 

Alternative 3 (file: GuiEvents3.java)

Create, for each button, a dedicated inner class that implements ActionListener and is responsible for handling each button's events
 public class GuiEvents3 extends JFrame {
    private void init() {
        ...
        this.button1 = new JButton("generate new number");
        this.button2 = new JButton("reset lable");

        //add a handler to the button1 action event:
        this.button1.addActionListener(new Button1Handler());
        this.button2.addActionListener(new Button2Handler());
        ...
    }
   
    ...
   
    private class Button1Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //any logic here
            updateLabelNumber(); //a method on the parent
        }
    }

    private class Button2Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //any logic here
            resetLabel(); //a method on the parent
        }
    }
}


Sem comentários:

Enviar um comentário