• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Java listener not working  RSS feed

 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little new to Java so it's probably a fundamental misunderstanding. I thought I got this kind of thing pretty well because I did it once and it worked perfectly. But now, when I copied the code and edited it for another custom listener I needed. I looked over it for a while and can't find the problem. Eclipse tells me it's at where I do the actual adding the listener but I don't see what's wrong. Here is the source code simplified. It has the same error message in the same spot.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never initialized _Listeners and therefore it's still null. However, you don't need that field. JComponent has a protected EventListenerList field called listenerList which is inherited by both JPanel and your own class. It's initialized from the JComponent constructors, and you can just use that.
 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You never initialized _Listeners and therefore it's still null. However, you don't need that field. JComponent has a protected EventListenerList field called listenerList which is inherited by both JPanel and your own class. It's initialized from the JComponent constructors, and you can just use that.


Thanks. I just figured it out a few moments ago and came back to say that exact thing. I was never told about the listenerList though. That will be nice since I use JPanels for most of my custom components.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!