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 6 of 6

Thread: What's the difference between "import.javax.swing*" and JFrame inheritance

  1. #1
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What's the difference between "import.javax.swing*" and JFrame inheritance

    Is it possible to use all the components of Swing class such as JFrame without extending an existing class to JFrame? if so why then programmers usually extend their classes to JFrame class and import swing at the same time?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: What's the difference between "import.javax.swing*" and JFrame inheritance

    JFrame is one class in the javax.swing package.
    Extending a class has nothing to do with what import statements you code.
    An import statement is used by the compiler to find the definition of a class that is defined in a package.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What's the difference between "import.javax.swing*" and JFrame inheritance

    Well, can I use JFrame simply by importing all of the swing classes without extending an existing class to JFrame? I am sorry if my question doesn't make sense but I am a newbie in java programming and in a tutorial few days back I saw someone making a JFrame only by importing all of the swing classes and he didn't extend the existing class to JFrame.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What's the difference between "import.javax.swing*" and JFrame inheritance

    Quote Originally Posted by Johnny Bravo View Post
    ... making a JFrame only by importing all of the swing classes and he didn't extend the existing class to JFrame.
    If you do that then this would fail:
    public JFrame jf = new ClassThatDoesNotExtendJFrame();//fail

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: What's the difference between "import.javax.swing*" and JFrame inheritance

    You do not need to use the import statement to use any of the java classes. You can code the full package/class name:
    public MyFrame extends javax.swing.JFrame { ...}
    The import statement tells the compiler where to look to find class definitions. It's sort of an extension of classpath. The import statement does not create any code in a program.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6

    Default Re: What's the difference between "import.javax.swing*" and JFrame inheritance

    using import statements is just a shortcut. At any time you can use fully qualified name of classes or interfaces.

Similar Threads

  1. [SOLVED] Adding the "final" keyword makes difference, I'm confused with the sequence flow.
    By SmokyBrain in forum Object Oriented Programming
    Replies: 2
    Last Post: April 30th, 2012, 01:50 AM
  2. Java tip Sep 22, 2010 - "Screenshots" of swing components
    By helloworld922 in forum Java Swing Tutorials
    Replies: 3
    Last Post: June 6th, 2011, 01:25 AM
  3. Java tip Sep 22, 2010 - "Screenshots" of swing components
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: June 6th, 2011, 01:25 AM
  4. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 02:09 PM
  5. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 02:52 PM