File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Troubleshooting an ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Troubleshooting an ActionListener" Watch "Troubleshooting an ActionListener" New topic
Author

Troubleshooting an ActionListener

De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Hey Ranchers,

I'm having an issue identifying the problem with this ActionListener. The event is to fire when the reset button is clicked and reset the value of my sliders to zero. Thank you in advance for pointing out my novice errors.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
And what goes wrong when you try it?
De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Campbell Ritchie wrote:And what goes wrong when you try it?


I get these ominous errors...

SoundLevels.java:119: cannot find symbol
symbol : class ActionListener
location: class SoundLevels
private class ResetButtonListener implements ActionListener
^
SoundLevels.java:121: cannot find symbol
symbol : class ActionEvent
location: class SoundLevels.ResetButtonListener
public void actionPerformed(ActionEvent e)
^
SoundLevels.java:84: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (SoundLevels.ResetButtonListener)
reset.addActionListener(new ResetButtonListener());
^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

ActionListener and ActionEvent are in package java.awt.event, not javax.swing.event.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Rob Spoor wrote:ActionListener and ActionEvent are in package java.awt.event, not javax.swing.event.


Thanks Rob, I think I changed this based on a tutorial somewhere. When I import this package, it then sends my Changelistener into a frenzy.

SoundLevels.java:105: cannot find symbol
symbol : class ChangeListener
location: class SoundLevels
private class SliderListener implements ChangeListener
^
SoundLevels.java:107: cannot find symbol
symbol : class ChangeEvent
location: class SoundLevels.SliderListener
public void stateChanged(ChangeEvent e)
^
SoundLevels.java:51: addChangeListener(javax.swing.event.ChangeListener) in javax.swing.JSlider cannot be applied to (SoundLevels.SliderListener)
slideLeft.addChangeListener(new SliderListener());
^
SoundLevels.java:56: addChangeListener(javax.swing.event.ChangeListener) in javax.swing.JSlider cannot be applied to (SoundLevels.SliderListener)
slideRight.addChangeListener(new SliderListener());
^
4 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

I didn't say you had to remove the javax.swing.event import. I must admit it's a bit confusing, with some event listeners being in java.awt.event and others in javax.swing.event. The distinction is that those in java.awt.event are the "core" event listeners that are also used by AWT, and those in javax.swing.event are for Swing only.
De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Rob Spoor wrote:I didn't say you had to remove the javax.swing.event import. I must admit it's a bit confusing, with some event listeners being in java.awt.event and others in javax.swing.event. The distinction is that those in java.awt.event are the "core" event listeners that are also used by AWT, and those in javax.swing.event are for Swing only.


Thanks for the explanation, it now compiles and behaves as expected

I'll research and commit to memory those which are "core" and those that are swing only. For now is there any harm including them both in my default import when building GUIs in Java?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
De Huaste wrote: . . . I'll research and commit to memory those which are "core" and those that are swing only. . . .
Don’t do that. There are something like 4500 classes in the standard Oracle/Sun Java API, so unless you expect to learn them all off by heart, that will be a useless task. What you need to know is how to find those classes in the API. Look here, and you will see it is says
java.awt.event
Interface ActionListener
So now you know to import it in the package java.awt.event. You ought to bookmark the index to the API documentation because you will use it all the time.

If you import something you don’t use, 99% of the time this is what will happen: Nothing.
You can occasionally have problems. Try importing java.util.* and javax.swing.* and using the Timer class
When you have worked out what went wrong, you will find out why many people prefer to import individual classes rather than on‑demand imports. More about imports here.

I would have known you had the wrong import, too, had you said it was a compiler error. But you didn’t, so I looked at your code wondering what the logic error was and couldn’t find it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Campbell Ritchie wrote:
You can occasionally have problems. Try importing java.util.* and javax.swing.* and using the Timer class

Or worse: import java.awt.* and java.util.* and try to use List...
De Huaste
Greenhorn

Joined: Nov 18, 2012
Posts: 11
Campbell Ritchie wrote:
De Huaste wrote: . . . I'll research and commit to memory those which are "core" and those that are swing only. . . .
Don’t do that. There are something like 4500 classes in the standard Oracle/Sun Java API, so unless you expect to learn them all off by heart, that will be a useless task. What you need to know is how to find those classes in the API. Look here, and you will see it is says
java.awt.event
Interface ActionListener
So now you know to import it in the package java.awt.event. You ought to bookmark the index to the API documentation because you will use it all the time.

If you import something you don’t use, 99% of the time this is what will happen: Nothing.
You can occasionally have problems. Try importing java.util.* and javax.swing.* and using the Timer class
When you have worked out what went wrong, you will find out why many people prefer to import individual classes rather than on‑demand imports. More about imports here.

I would have known you had the wrong import, too, had you said it was a compiler error. But you didn’t, so I looked at your code wondering what the logic error was and couldn’t find it.


As soon as I posted, I knew it was an overstatement ;)

Thanks for all the advice, I have bookmarked the API reference. Apologies for not being more specific with my error, I appreciate you taking the time to look at the code. This is an outstanding board!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Rob Spoor wrote: . . . Or worse: import java.awt.* and java.util.* and try to use List...
Is that really worse than my example? I think the two will be as bad as each other.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Consider the number of times you use Timer. Now consider the number of times you use List. For me, the ratio would be largely in favour of java.util.List.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Troubleshooting an ActionListener
 
Similar Threads
: A gray color box appeare d when loading an image on JPanel
JSlider update another JSlider problems
How to refresh the JButton text among other things.
Illegal argument exception: cannot add parent to itself
Navigating between two JFrame in Java Swing