0

You can read all over the web that AWT is old and deprecated and Swing is old but newer than AWT and should be preferred over AWT whenever possible. But how can I identify when it is possible to replace AWT components with its Swing pendants? Several examples in the web use still AWT components where Swing could be used instead. So is there a clear advice what to use from AWT and what not? I know that the java compiler will give a short note when I use officially deprecated components, e.g.:

import java.awt.*;

public class Hello {
  public static void main( String[] args ) {
    Frame f = new Frame( "Hello" );
    f.setSize( 300, 300 );
    f.show();
  }
}

will produce an warning like this:

Note: Hello.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

and with -Xlint:

Hello.java:9: warning: [deprecation] show() in Window has been deprecated
    f.show();
     ^
1 warning

But as far as I understood the web and oracle, I prefer to use JComponents over AWT components. But the following code will not notice me to use JFrame instead of Frame:

import java.awt.*;

public class Hello {
  public static void main( String[] args ) {
    Frame f = new Frame( "Hello" );
    f.setSize( 300, 300 );
    f.setVisible( true );
  }
}

All in all I am curious when I read code in the web, if it is up to date and follows the principle to use Swing where ever possible, or if I need to improve.

In other words: What are the safe parts of java.awt.* and its subpackages?

Thanks for your thoughts and advisory.

Community
  • 1
  • 1
math
  • 8,514
  • 10
  • 53
  • 61

1 Answers1

2

There's no need for you to use AWT in this day and age, unless you can come up with a good reason. You're obviously reading outdated (or just plain horrible) examples on the web, if there's something like frame.show() involved.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • For example: Are there replacements within Swing for `java.awt.event.*` or `BorderLayout`? So why does the offical tutorial from oracle still involve AWT? – math Jan 22 '14 at 09:39
  • Events and Layouts aren't components. There's nothing wrong with using them. – Kayaman Jan 22 '14 at 09:40
  • Ahh, its all about Components, ONLY? As there is also a `javax.swing.event.*` package.. – math Jan 22 '14 at 09:42
  • 1
    AWT includes a lot of non-component things that we still need in Swing apps. `Color`, `Dimension`, many layouts, printing, Drag-and-Drop, Java 2D.. As to Swing being 'old' I disagree with that term. It is simply 'mature'. There are various things added to Swing with each new major Java version. – Andrew Thompson Jan 22 '14 at 10:38
  • I think that comment would be worth as an answer. – math Jan 23 '14 at 09:23