Necessity of importing java.awt.event.*
I am just starting to mess around with Java's GUI classes specifically with event handling and ask myself a question when I saw that a class that I was implementing didn't recognize its super abstract class.
In short, Why is it necessary to code the below, for the private class WindowEventHandler to implement the abstract class ActionListener?
Code java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Window extends JFrame
{
// Vars here
public Window() throws HeadlessException
{
// Window Constructor
}
private class WindowEventHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
Why is it not possible to just have the statement :import java.awt.*; and not both import java.awt.*; and import java.awt.event.*;
Are these two classes not from the same package? I realize that it is a inheritance concept that I don't understand. Can anyone help?
Re: Necessity of importing java.awt.event.*
Yes, they are in different packages: ActionEvent is in the java.awt.event package - the imports do not cascade down the directory structure (so importing java.awt.* does not implicitly import java.awt.event package). See the following link for a tutorial on packages:
Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)
1 Attachment(s)
Re: Necessity of importing java.awt.event.*
Ah! Got it. The asterisk and import statement implicitly calls the package referenced, not necessarily the name of the classes that follow the directory structure.
Thanks very much!Attachment 936
Re: Necessity of importing java.awt.event.*
The best practice is using fully qualified import names for import statements, use the wildcards only if you are importing a large number of classes inside a package.
1 Attachment(s)
Re: Necessity of importing java.awt.event.*
@nickyc
Yes of course! That makes perfect sense. NO doubt if you creating a application that requires on ly one or two classes out the entire package, it would efficient specifying only those classes. Thank you for pointing that out.
Here have some cake too Attachment 937