Results 1 to 6 of 6
  1. #1
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Relationship Between java.awt and javax.swing

    Hey guys, I'm currently working through an introduction to javax.swing so right now I'm trying to wrap my head around the basic concepts. I'm especially confused by the relationship between java.awt.Component and javax.swing.JComponent.

    Component has subclasses called Container, Label, Button, ScrollBar, ect. JComponent has subclasses called JContainer, JLabel, JButton, JScrollBar, ect. Since JComponent inherits from Component, why does it create what seem to me like duplicate classes?

    In fact, javax.swing seems to be simply a more extensive mirror of java.awt. Did java.awt not effectively produce graphics in Java by itself?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Relationship Between java.awt and javax.swing

    Swing is a newer gui API. Swing's implementation is in terms of Java code whereas AWT components depend on platform specific code for their implementation.

    Essentially Swing replaced AWT in the sense that nowadays your prefer JWhatever to the older Whatever. But none of the old API was removed (ie existing code was not broken with the change to Swing) and, in fact, Swing components are typically subclasses of the AWT Container class. A slightly messy situation, perhaps, but typical of anything that evolves (ie whose design has a history.)

    There's an interesting overview article in Wikipedia.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Relationship Between java.awt and javax.swing

    Ok, thank you for the info! Here's my follow-up question, though...if java.awt was programmed using machine-specific code, how can javax.swing be coded purely in Java if its classes extend the classes within java.awt?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Relationship Between java.awt and javax.swing

    There's going to be some machine specific code somewhere!

    Swing uses as little as possible, preferring to implement the functionality of buttons, textfields etc in Java code. As that article describes, the Swing components extend Container which - I would imagine - is less machine specific than Button, TextField etc. The exception (again, described in that article) are the top level containers (windows, dialogs, frames and applets) which do extend their AWT forebears and inherit their dependence on a machine specific implementation.

    The point is, that it's not all or nothing. Swing significantly increased the amount of implementation done in Java, rather than being handed over to machine specific code.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Relationship Between java.awt and javax.swing

    Quote Originally Posted by awinston View Post
    Component has subclasses called Container, Label, Button, ScrollBar, ect. JComponent has subclasses called JContainer, JLabel, JButton, JScrollBar, ect.
    No. There's no JContainer. JComponent inherits from Container.

    The AWT GUI elements ('heavyweight components') are obtained from the underlying OS. In Swing, only top-level windows (JFrame, JDialog, JWindow and the windows created by the Swing code for showing any menu/popup/JComboBox drop-down that protrudes outside the top-level window it's a part of, are heavyweight.

    Swing components are 'lightweight' -- they are created entirely by painting in the client space of the parent window. Since there may be several layers of components, there may also be several layers of painting. See the API for JRootPane and JScrollPane to start to get an understanding of what that may entail.

    In Windows, if you use a Microsoft utility called Spy++ you can discover that each AWT Button, TextField etc. is its own 'window' whereas in Swing there's only the one 'window'

    Search the net using keywords like 'heavyweight vs lightweight components' / 'painting in AWT and Swing' / 'Mixing heavyweight and lightweight components' and you'll find some interesting reads. While you're about it, and as you said you're just starting in Swing, also search for Swing's single threaded rule -- it's a good idea to learn about that earlier rather than after it trips you up.

    Since this is all about AWT and Swing, I'm moving it out of New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Relationship Between java.awt and javax.swing

    Quote Originally Posted by DarrylBurke View Post
    No. There's no JContainer. JComponent inherits from Container.
    You're right, I got confused by the use of "container" to describe JFrame and such.

    Thanks for the info, pbrockway2 and db.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. It Is Possible for javax.swing.Icon into java.awt.File? as stream
    By raj.mscking@gmail.com in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 09:22 PM
  2. java.awt.TrayIcon & javax.swing.JPopupMenu
    By Seyed in forum AWT / Swing
    Replies: 0
    Last Post: 03-18-2011, 08:58 PM
  3. Replies: 6
    Last Post: 05-27-2009, 02:17 AM
  4. Replies: 6
    Last Post: 07-10-2008, 08:28 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •