Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarification --> Import statement  RSS feed

 
Adinarayana Pabolu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is both of these statements required to run any program having event handling in swing ?
import java.awt.*;
import java.awt.event.*;
or
import java.awt.*; is alone will do ? Why?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if the Swing program uses any classes out of AWT, or any AWT event classes (which it most probably will... ).

When you import java.awt.*, you import all the classes in the java.awt package, but no sub-packages of java.awt (such as java.awt.event). You have to import each package separately. As for why? I guess that's just how James Gosling wanted to work imports when he made the Java language specification. (Actually, though, it's a good thing to specifically import packages and not have them load subpackages. You have no idea how deep the subpackages go... think what would happen if you said "import java.*" in this situation! )

In your case, if you only need access to the AWT events in your class, you can just import java.awt.event, you don't have to import java.awt.
[ December 26, 2002: Message edited by: Nathan Pruett ]
 
Adinarayana Pabolu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!