Abstract Window Toolkit: Difference between revisions

Content deleted Content added
m date format audit, minor formatting, typo(s) fixed: For example → For example,
 
(18 intermediate revisions by 14 users not shown)
Line 1:
{{Short description|Java-based GUI toolkit}}
{{Use dmy dates|date=June 2021}}
[[File:Easy Java AWT example.jpg|thumb|250px|Windows form with some AWT examples]]
 
The '''Abstract Window Toolkit''' ('''AWT''') is [[Java (programming language)|Java]]'s original platform-dependent [[Windowing system|windowing]], [[graphic]]s, and [[user-interface]] [[widget toolkit]], preceding [[Swing (Java)|Swing]]. The AWT is part of the [[Java Foundation Classes]] (JFC) — the standard [[Application programming interface|API]] for providing a [[graphical user interface]] (GUI) for a Java program.<ref name="foldoc">{{foldoc|Abstract+Window+Toolkit}}</ref> AWT is also the GUI toolkit for a number of [[Java Platform, Micro Edition|Java ME]] profiles. For example, [[Connected Device Configuration]] profiles require Java [[software execution|runtime]]s on [[mobile telephone]]s to support the Abstract Window Toolkit.
AWT is also the GUI toolkit for a number of [[Java Platform, Micro Edition|Java ME]] profiles. For example, [[Connected Device Configuration]] profiles require Java [[software execution|runtime]]s on [[mobile telephone]]s to support the Abstract Window Toolkit.
 
== History ==
When [[Sun Microsystems]] first released Java in 1995, AWT widgets provided a thin level of abstraction over the underlying native user-interface. For example, creating an AWT [[check box]] would cause AWT directly to call the underlying native subroutine that created a check box. However, athe check box on [[Microsoft Windows]] is not exactly the same as athe check box on [[Mac OSmacOS]] or on the various types of [[Unix]]. Some application developers prefer this model because it provides a high degree of fidelity to the underlying native windowing toolkit and seamless integration with native applications. In other words, a GUI program written using AWT looks like a native Microsoft Windows application when run on Windows, but the same program looks like a native [[Apple Macintosh]] application when run on a Mac, etc. However, some application developers dislike this model because they prefer their applications to look exactly the same on every platform.
 
In [[Java Platform, Standard Edition|J2SE 1.2]], the [[Swing (Java)|Swing]] toolkit largely superseded the AWT's widgets. In addition to providing a richer set of UI widgets, Swing draws its own widgets (by using [[Java 2D]] to call into low-level subroutines in the local graphics subsystem) instead of relying on the operating system's high-level user interface module. Swing provides the option of using either the native platform's [[Look and feel#Look and Feel in Widget Toolkits|"look and feel"]] or a cross-platform look and feel (the "Java Look and Feel") that looks the same on all windowing systems.
Line 18:
** Several [[layout manager]]s;
** The interface to [[input device]]s such as [[Mouse (computing)|mouse]] and [[Keyboard (computing)|keyboard]]; and
** A {{Javadoc:SE|package=java.awt.datatransfer|java/awt/datatransfer|module=java.desktop}} [[Java package|package]] for use with the [[Clipboard (software)|Clipboard]] and [[Drag and drop|Drag and Drop]].
* A basic set of GUI widgets such as buttons, text boxes, and menus. It also provides the [[Java AWT Native Interface|AWT Native Interface]], which enables [[Rendering (computer graphics)|rendering]] [[library (computer science)|libraries]] compiled to [[native code]] to draw directly to an AWT {{Javadoc:SE|java/awt|Canvas|module=java.desktop}} [[object (computer science)|object]] drawing surface.
 
AWT also makes some higher level functionality available to applications, such as:
Line 25:
* The ability to launch some desktop applications such as [[web browser]]s and [[Mail client|email clients]] from a Java application.
 
Neither AWT nor Swing areis inherently [[thread safety|thread safe]]. Therefore, code that updates the GUI or processes events should execute on the [[Event dispatching thread]]. Failure to do so may result in a [[Deadlock (computer science)|deadlock]] or race condition. To address this problem, a utility class called [[SwingWorker]] allows applications to perform time-consuming tasks following user-interaction events in the event dispatching thread.
 
== Mixing AWT and Swing components ==
Line 33:
When drawing in Swing, use JPanel and override paintComponent(Graphics g) instead of using the AWT paint() methods.
 
Prior toBefore [[Java version history#Java SE 6 .28December 11.2C 2006.29|Java 6 Update 12]], mixing [[Swing (Java)|Swing]] components and basic AWT widgets often resulted in undesired side effects, with AWT widgets appearing on top of the Swing widgets regardless of their defined [[z-order]]. This problem was because the rendering architecture of the two widget toolkits was very different, despite Swing borrowing heavyweight top [[Container (data structure)#Graphic containers|containers]] from AWT.<ref>{{cite web
|url=http://java.sun.com/products/jfc/tsc/articles/mixing/index.html
|title=Mixing heavy and light components
Line 46:
}}</ref>
 
Starting in [[Java version history#Java SE 6 Update 10|Java 6 Update 12]], it is possible to mix Swing and AWT widgets without having z-order problems.<ref>{{cite web
|url=http://download.java.net/jdk6/6u12/promoted/b02/changes/jdk6uN-b02.html
|title=Bug/RFE fixed in current JDK 6u12 build
Line 59:
== Example ==
<syntaxhighlight lang="java">
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*WindowAdapter;
import java.awt.event.WindowEvent;
 
public class AppletApplication extends Applet implements WindowListenerMyApp {
 
public static void main(java.lang.String[] args) {
AppletApplicationFrame appletframe = new AppletApplicationFrame("Application");
Frame w = new Frame("Applet");
wframe.addWindowListeneradd(appletnew Label("Hello!"));
wframe.addsetSize("Center"500, applet500);
frame.setLocationRelativeTo(null); // Centers the window
w.setSize(50, 50);
w.setVisible(true);
appletframe.initaddWindowListener(new WindowAdapter(); {
applet.start(); @Override
public void windowClosing(WindowEvent e) {
frame.dispose(); // Releases native screen resources
}
});
}
frame.setVisible(true);
}
 
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello World", 10, 10); // Upper left corner
}
 
public void windowClosing(WindowEvent e) {
System.exit(0); // Exit the application when the window is closed
}
// Required methods
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
 
}
</syntaxhighlight>
Line 129 ⟶ 117:
|date=20 September 2009
|publisher=openjdk.java.net
|quote=''You don't need anymore of those patches, with the latest FontManager push, everything is upstream now, so just use the Cacio repo, it's completely self contained.''
|access-date=7 March 2010
|archive-url=https://web.archive.org/web/20120319173102/http://mail.openjdk.java.net/pipermail/caciocavallo-dev/2009-September/000184.html
Line 156 ⟶ 144:
 
== References ==
{{FOLDOCReflist}}
{{Reflist|30em}}
 
== External links ==
{{Wikibooks|Java Swings/AWT}}
{{Commons|AWT}}
* {{Javadoc:SE|package=java.awt|java/awt|module=java.desktop}} (AWT [[Javadoc]] API documentation)
* {{Javadoc:SE-guide|awt|AWT documentation}}
*[https://web.archive.org/web/20161215114326/http://www.java-forums.org/awt-swing/ AWT/Swing]
*[http://www.java-tips.org/java-se-tips-100019/21-java-awt.html java.awt] {{Webarchive|url=https://web.archive.org/web/20160829004424/http://www.java-tips.org/java-se-tips-100019/21-java-awt.html |date=29 August 2016 }}
 
{{Java desktop}}
{{Widget toolkits}}
 
[[Category:Articles with example Java code]]
[[Category:JavaJDK APIscomponents]]
[[Category:Widget toolkits]]