Abstract Window Toolkit: Difference between revisions

Content deleted Content added
 
(37 intermediate revisions by 30 users not shown)
Line 1:
{{Short description|Java-based GUI toolkit}}
{{Use dmy dates|date=AprilJune 20122021}}
[[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 ==
 
Prior to [[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
Where there is a Swing version of an AWT component it will begin with J- and should be used exclusively, replacing the AWT version. For example, in Swing, only use JButton, never Button class. As mentioned above, the AWT core classes, such as Color and Font, are still used as-is in Swing.
 
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 35 ⟶ 40:
|first=Amy
|publisher=[[Sun Microsystems]]
|accessdateaccess-date=17 December 2008
|archiveurlarchive-url=https://wwwweb.webcitationarchive.org/67LJuQLZ8?url=web/20111223035101/http://java.sun.com/products/jfc/tsc/articles/mixing/index.html
|archive-date=23 December 2011
|archivedate=1 May 2012
|url-status=live
|deadurl=no
|df=dmy
}}</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
|date=12 December 2008
|publisher=[[Sun Microsystems]]
|accessdateaccess-date=17 December 2008
|deadurlurl-status=yesdead
|archiveurlarchive-url=https://web.archive.org/web/20081217062847/http://download.java.net/jdk6/6u12/promoted/b02/changes/jdk6uN-b02.html
|archivedatearchive-date=17 December 2008
}}</ref>
 
== Example ==
<sourcesyntaxhighlight 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 WindowListener {
 
public static void main(java.lang.String[] args) {
AppletApplication applet = new AppletApplication();
Frame w = new Frame("Applet");
w.addWindowListener(applet);
w.add("Center", applet);
w.setSize(50, 50);
w.setVisible(true);
applet.init();
applet.start();
}
 
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello World", 10, 10); // Upper left corner
}
 
public void windowClosing(WindowEventclass e)MyApp {
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) { }
 
public static void main(java.lang.String[] args) {
Frame wframe = new Frame("AppletApplication");
frame.add(new Label("Hello!"));
w frame.setSize(50500, 50500);
frame.setLocationRelativeTo(null); // Centers the window
w frame.addWindowListener(appletnew WindowAdapter(); {
@Override
public void windowActivatedwindowClosing(WindowEvent e) { }
frame.dispose(); // Releases native screen resources
}
});
w frame.setVisible(true);
}
}
</syntaxhighlight>
</source>
 
== Implementation ==
Line 101 ⟶ 93:
|last=Torre
|first=Mario
|accessdateaccess-date=7 September 2008
|archiveurlarchive-url=https://wwwweb.webcitationarchive.org/67LJv4s5V?url=web/20120319173058/http://mail.openjdk.java.net/pipermail/challenge-discuss/2008-March/000082.html
|archivedatearchive-date=119 MayMarch 2012
|url-status=live
|deadurl=no
|df=dmy
}}</ref><ref>{{cite web
| url=http://rkennke.wordpress.com/2008/12/18/caciocavallo-architecture-overview/
Line 111 ⟶ 102:
| date = 18 December 2008
| last=Kennke|first=Roman
| accessdateaccess-date=7 September 2008}}</ref> The project has successfully implemented AWT widgets using [[Java2D]].<ref>{{cite web
|url=http://rkennke.wordpress.com/2008/09/03/cacio-swing-awt-peers/
|title=Cacio Swing AWT peers
Line 117 ⟶ 108:
|last=Kennke
|first=Roman
|accessdateaccess-date=7 September 2008
|archiveurlarchive-url=https://wwwweb.webcitationarchive.org/6GEGSl3wV?url=web/20120313203649/http://rkennke.wordpress.com/2008/09/03/cacio-swing-awt-peers/
|archive-date=13 March 2012
|archivedate=29 April 2013
|url-status=live
|deadurl=no
|df=dmy
}}</ref> All the necessary core-JDK modifications have since been pushed to [[OpenJDK|OpenJDK 7]],<ref>{{cite web
|url=http://mail.openjdk.java.net/pipermail/caciocavallo-dev/2009-September/000184.html
Line 127 ⟶ 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.''
|accessdateaccess-date=7 March 2010
|archiveurlarchive-url=https://wwwweb.webcitationarchive.org/67LJvUzIH?url=web/20120319173102/http://mail.openjdk.java.net/pipermail/caciocavallo-dev/2009-September/000184.html
|archivedatearchive-date=119 MayMarch 2012
|url-status=live
|deadurl=no
|df=dmy
}}</ref> which means that Java can now be used on a graphics stack other than one of those provided by the official JDK ([[X Window System]], [[OpenGL]] or [[DirectX]]), by including an external library and setting some system properties. A [[DirectFB]] backend for Caciocavallo<ref name="rkennkeDirectFB">{{cite web
| url=http://rkennke.wordpress.com/2011/07/28/jdk7-and-cacio-coolness/
| title=JDK7 and Cacio coolness
| date=28 July 2011
| accessdateaccess-date=8 August 2011
| last=Kennke|first=Roman}}</ref> is under development, as is an [[HTML5]] backend; the aim is to deploy existing Swing applications—without Java support—as ordinary web applications running on a web server.<ref name="rkennkeDirectFB" /><ref>{{cite web
|url=http://www.google-melange.com/gsoc/project/google/gsoc2011/linuxhippy/11001
|title=HTML5/Canvas backend for Caciocavallo (GNU-Classpath)
|accessdateaccess-date=8 August 2011
|last=Eisserer
|first=Clemens
|archiveurlarchive-url=https://wwwweb.webcitationarchive.org/60p6ELb1M?url=web/20120321144746/http://www.google-melange.com/gsoc/project/google/gsoc2011/linuxhippy/11001
|archive-date=21 March 2012
|archivedate=10 August 2011
|url-status=dead
|8=
|deadurl=yes
|df=dmy
}}</ref>
 
== See also ==
{{Portal|JavaComputer programming}}
* [[Swing (Java)]]
* [[Standard Widget Toolkit]]
 
== 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:Java platform]]
[[Category:Widget toolkits]]