Mohsen Kamrani
Mohsen Kamrani

Reputation: 7457

How to add a listener

I hardly struggle to add a custom event to my textarea, but after a couple of days I still cannot handle it!!! I've read many resources and finally I could write the code below.

Problem

I think everything is right but I just add the listener wrongly.(Maybe something else is wrong). I don't know how to fix it or is it the cause of the problem at all?

Purpose

I want to make this work (for test) and then use it inside my main project, so the code below is just for avoiding irrelevant things.

Scenario

I want the button to fire a custom event that my textarea listens to. Please consider that (due to my main project design) I have to define and add the textarea inside a method and that's the main source of my problem. But unfortunately I cannot change this decision.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.EventObject;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.EventListenerList;

public class CustomEvent extends JFrame {
    private transient Vector listeners;
    private JPanel contentPane, panel;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CustomEvent frame = new CustomEvent();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public CustomEvent() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                fireEvent("yes");
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);
        fillPanel();
        contentPane.add(panel, BorderLayout.CENTER);
    }
    void fillPanel() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout(0, 0));
        final MyTextArea textArea;
        textArea = new MyTextArea();
        //textField should listen to customEventObject
        panel.add(textArea, BorderLayout.CENTER);
        textArea.setColumns(10);
        textArea.addMyListener(new MyListener() {
            @Override
            public void actionPerformed(MyEvent e) {
                System.out.println(e.getArg());

            }
        });
    }
    protected void fireEvent(String str) {
        // if we have no listeners, do nothing...
        if (listeners != null && !listeners.isEmpty()) {
            // create the event object to send
            MyEvent event = new MyEvent(this, str);

            // make a copy of the listener list in case
            //   anyone adds/removes listeners
            Vector targets;
            synchronized (this) {
                targets = (Vector) listeners.clone();
            }

            // walk through the listener list and
            //   call the sunMoved method in each
            Enumeration e = targets.elements();
            while (e.hasMoreElements()) {
                MyListener l = (MyListener) e.nextElement();
                l.actionPerformed(event);
            }
        }
    }
}
class MyTextArea extends TextArea implements Serializable {
    private transient Vector listeners;
    synchronized public void addMyListener(MyListener l) {
        if (listeners == null)
            listeners = new Vector();
        listeners.addElement(l);
    }  
    synchronized public void removeMyListener(MyListener l) {
        if (listeners == null)
            listeners = new Vector();
        listeners.removeElement(l);
    }
}
//--------------------------------------------------------------
interface MyListener extends EventListener {
    public void actionPerformed(MyEvent e);
}
//--------------------------------------------------------------
class MyEvent extends EventObject {
    private String arg;
    public MyEvent(Object source, String arg) {
        super(source);
        this.arg = arg;
    }
    public String getArg() {
        return this.arg;
    }
}

Upvotes: 3

Views: 7221

Answers (1)

alsed42
alsed42

Reputation: 1216

First of all, you're not adding any listeners to the listener field in CustomListener. Secondly, the listeners in MyTextArea are never called.

Edit

To clarify:

You have a Vector of listeners in your MyTextArea class but there's no method to fire the event. From the way I understand your problem, you want MyTextArea to be a listener of CustomEvent, but you designed it like a class that generates events, not one that receives events. So I would change MyTextArea like this:

class MyTextArea extends TextArea implements MyListener {

    @Override
    public void actionPerformed(MyEvent e) {
        System.out.println(e.getArg());
    }
}

Now that MyTextArea implements MyListener, you can add it to the list of listeners in CustomEvent.fillPanel():

void fillPanel() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout(0, 0));
    final MyTextArea textArea;
    textArea = new MyTextArea();
    //textField should listen to customEventObject
    panel.add(textArea, BorderLayout.CENTER);
    textArea.setColumns(10);
    listeners.add(textArea);
}

Upvotes: 1

Related Questions