Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Necessity of importing java.awt.event.*

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Location
    In a nicely heated dwelling
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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?

    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?
    Last edited by Dracone Cordis; January 8th, 2012 at 12:07 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Location
    In a nicely heated dwelling
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!bday_cake.gif

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Location
    In a nicely heated dwelling
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 bday_cake.gif

Similar Threads

  1. how to implement timed out event in java?!
    By migongotar in forum Java Theory & Questions
    Replies: 2
    Last Post: August 9th, 2011, 05:56 AM
  2. Tic Tac Toe Java Application Help w/ Importing Text Field
    By Big Bundy in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 30th, 2011, 02:08 AM
  3. Importing gif/pictures
    By javanerd in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2010, 11:58 PM
  4. Java onLoad event after validation is complete
    By macrat101 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 4th, 2010, 03:14 PM
  5. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 04:49 PM

Tags for this Thread