Crystal
Crystal

Reputation: 29518

Making your class an event source in Java

I'm making a custom button in Java that has two states, mousePressed, and mouseReleased. At the same time, if I wanted to reuse this button, so that other event listeners can register with it, are these the appropriate steps I should do (This is a hw assignment so although a JButton could be used, I think we are trying to show that we can create our own Button to act like JButton:

I'm getting the compile errors: line 38: reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match List listenerList = new ArrayList();

and line 34: cannot find symbol, method actionPerfomed() in interface java.awt.event.ActionListener action.actionPerformed();

Upvotes: 0

Views: 711

Answers (2)

camickr
camickr

Reputation: 324167

I'm making a custom button in Java that has two states, mousePressed, and mouseReleased

Maybe you should be using a JToggleButton.

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

No, completely not!

A JButton has everything you need. Just add your own listener to the button. Don't override something. Just like this:

public class MyButton extends JButton implements MouseListener // maybe you want to add other listeners... separate them with comma's.
{
     public MyButton(String caption)
     {
         super(caption);
         addMouseListener(this);
     }

     // implement your listener methods here

}

Upvotes: 1

Related Questions