AWT Tutorials

AWT Tutorials

In this detailed Resource page, we feature an abundance of AWT Tutorials!

The Abstract Window Toolkit (AWT) is Java’s original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support the Abstract Window Toolkit.

Architecture

The AWT provides two levels of APIs:

A general interface between Java and the native system, used for windowing, events, and layout managers. This API is at the core of Java GUI programming and is also used by Swing and Java 2D. It contains:

  • The interface between the native windowing system and the Java application
  • The core of the GUI event subsystem
  • Several layout managers
  • The interface to input devices such as mouse and keyboard
  • A java.awt.datatransfer package for use with the Clipboard and Drag and Drop.

A basic set of GUI widgets such as buttons, text boxes, and menus. It also provides the AWT Native Interface, which enables rendering libraries compiled to native code to draw directly to an AWT Canvas object drawing surface.

AWT also makes some higher level functionality available to applications, such as:

  • Access to the system tray on supporting systems
  • The ability to launch some desktop applications such as web browsers and email clients from a Java application.

Neither AWT nor Swing are inherently 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 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 6 Update 12, mixing 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 containers from AWT.

Starting in Java 6 Update 12, it is possible to mix Swing and AWT widgets without having z-order problems.

Implementation

As the AWT is a bridge to the underlying native user-interface, its implementation on a new operating system may involve a lot of work, especially if it involves any of the AWT GUI widgets, because each of them requires that its native peers be developed from scratch.

A new project, Caciocavallo, has been created, that provides an OpenJDK-based Java API to ease AWT implementation on new systems. The project has successfully implemented AWT widgets using Java2D. All the necessary core-JDK modifications have since been pushed to OpenJDK 7, 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 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.

AWT Tutorials – Getting Started

Simple examples based on AWT

  • Creating basic shapes example
    In this tutorial we shall show you how to construct a simple graphic by creating basic shapes. We are going to use some the built in classes that Java offers.
  • Print shapes example
    In this tutorial we are going to show how you can print simple shapes in paper using your printer. We are going to use some basic classes from AWT.
  • Scaling Shearing Translating Rotating a Shape
    With this example will shall show you four fundamental graphical transformations that you should know when you try to construct you own custom graphical environment or develop simple graphics.
  • Combining shapes example
    In this tutorial we shall show you how to create a simple graphic by combining basic shapes. We are going to use some the built in classes that Java offers.
  • List all available font families
    With this tutorial we are going to see how to list all available font families in your environment using Java.
  • Ring the bell example
    In this tutorial we are going to show you how to use the Terminal bell in a Java program. You might want to use the bell in terminal based apps to get the user’s attention when something important happens to the program.
  • Setting the initial focus component in a Window
    With this example we shall show you how to set the component that gets the focus when your application starts up. This is very useful when you have many graphical objects firing up at the start up of your application.
  • Stroking or Filling with a Texture example
    With this example we shall show you how stroke or fill a simple graphic component with a texture.
  • Retrieve display device info
    In this example we are going to see how to retrieve information about the display of your device. You can use this in your application in order to present to the user all the information he needs to know about the display he is working on.
  • Create a Container example
    With this tutorial we shall show you how to create a Container in a Java Desktop Application. The Container component in a very important one because it gives you the ability to organize and group together the components you want.
  • RGB to HSB and vice versa color conversion
    With this example we are going to see how to convert RGB to HSB and vice versa. This is very useful when you want to unify the units in Java Desktop application, and thus make the components much more manageable.
  • Screen capture example
    In this example we are going to see how to take screen captures in a Java Desktop Application
  • Getting and setting text on the system clipboard
    With this example we are going to see how to get and set the text on the system clipboard. This is a very common feature that we all use every day when working on a computer.
  • Drag and Drop example
    In this tutorial we are going to see how to build a simple application that performs drag and drop operations in a Java Desktop Application. As you know the drag and drop operation is very important in an Application that has to deal with graphical objects.
  • Java GridLayout Example
    In this example we are going to design a simple calculator using GridLayout, where the grid will contains simple components.

AWT Tutorials – Frame

Basic functionality examples based on AWT frame

  • Center Frame on screen example
    In this example we are going to see how to center a Frame on the screen. This is very important when you have many windows opened to your application and you want to manage the important ones.
  • Create a Frame example
    In this example we are going to show you how to create a Frame in a Java Desktop Application. This is a very important part of creating your own graphics for the applications you build. The Frame is the single most important component you have to use in your application.
  • Hide Frame when closed example
    With this example we are going to see how to hide a closed frame.
  • Exit application when Frame is closed example
    With this tutorial we shall show you how to exit from your application when a frame is closed. This is a very common use for most GUI applications.
  • Determining when a frame is Iconized or Maximized or Opened or Closed
    With this example we are going to see how to determine when a frame is Iconized or Maximized or Opened or Closed.

AWT Tutorials – Image

Basic functionality examples based on AWT image

  • Scaling Shearing Translating and Rotating a drawn image
    With this example will shall show you four fundamental graphical transformations that you should know when you try to construct you own custom graphical environment or develop simple graphics.
  • Drawing an image example
    In this tutorial we are going to show you how to draw on an Image. This is quite useful when you want to further customize the graphics of your application, if you are not pleased with the original Image.
  • Loading an Image from a file
    In this tutorial we are going to show you how to draw on an Image. This is quite useful when you want to further customize the graphics of your application, if you are not pleased with the original Image.
  • Draw using Antialiasing example
    In this example we are going to see how to draw an image with Antialiasing enabled. The notion of antialiasing is one of the most famous among the graphics world.
  • Draw using Alpha example
    In this example we are going to see how to draw an image with Alpha enabled. The notion of Alpha is quite famous on the graphics world. This will help you to make sharper graphics and make your images look very clear and avoid pixelation.
  • Draw using the stroking pen example
    With this tutorial we shall show you how to draw using the stroking pen. This is a very handy feature if you want to create simple graphics, by yourself. You can also create some cool paint applications using that.
  • Draw text example
    With this example we are going to see how to draw text in a Java Desktop Application. This is very useful when you have lots of graphics components in your application and you want to display some text in it.
  • Draw shapes example
    With this tutorial we shall show you how to draw simple shapes in a Java Desktop Application. This is a very important step when designing your own graphics for your App.
  • Drawing components with color example
    With this tutorial we shall show you how to draw Components with color. This is a very important operation when you want to liven up the graphics of you application, and customize them as you want.
  • Drawing components example
    With this tutorial we shall show you how to draw simple shapes in a Java Desktop Application. This is a very important step when designing your own graphics for your App.
  • Draw mixed style text example
    In this tutorial you are going to see how can you draw a line of text that each word has a different style.
  • Draw and Drag rectangles
    In this example we are going to create a simple application in which you can draw a simple shape (rectangle for example ) and drag it around our canvas. This is a very nice feature to use if your application has many graphical object that the user needs to move around the screen very frequently.
  • Drawing on a Buffered Image
    In this tutorial we are going to show you how to Draw on a Buffered Image. This is quite useful when you want to further customize the graphics of your application, if you are not pleased with the original Image.
  • Sharpening a Buffered Image
    In this example we are going to see how can you load an image from a source (a URL for instance) and make it look sharper in your own screen and according to the graphics environment that it is displayed into.
  • Create a shape from lines and curves
    With this tutorial we shall show you how to swap RGB values in an Image. This is very useful when you are trying to create some effects to your Application.
  • Set a clipping area from a Shape example
    In this example we shall show you how to set a clipping area from a component, in this case an image. This is a very basic operations that you will need when you are working in Java desktop applications that have rich graphcis.
  • Flipping a Buffered Image
    With this example we are going to show you how to perform one of the most basic graphics transformations. When you perform flipping of an image you simply rotate the image.
  • Embossing a Buffered Image
    In this tutorial we are going to see how can you load an image from a source and emboss it according to your needs.
  • Converting a Colored Buffered Image to Grayscale
    With this tutorial we are going to see all that is necessary in order to load a colored image from a source and convert it to grayscale.
  • Brightening or Darkening an RGB Buffered Image
    In this tutorial we are going to see all that is necessary in order to brighten or darken an RGB Buffered Image from a source. This is one of the most basic image processing techniques that you will use when developing applications that involve images.
  • Blurring a Buffered Image
    In this tutorial we are going to see how can you load an image from a source and blur it. This is one of the most basic graphics effects that you are going to use when you begin with image processing.
  • Scaling Shearing Translating and Rotating a Buffered Image
    With this example we shall show you four fundamental graphical transformations that you should know when you try to construct you own custom graphical environment or develop simple graphics.
  • Get and set pixels on a Buffered Image
    In this tutorial we are going to show you how to set and get the RGB value of each pixel of a Buffered Image. This is particularly useful when you want to perform several operation on images based on the RGB values of each individual image.
  • Java: Create BufferedImage from Image example
    In this tutorial we are going to show you how to create a Buffered Image from a source Image. This is a basic operation if you want to perform several enchantments and transformations to your Image and its fundamental for Image processing.
  • Swapping the RGB Values of an Image
    With this tutorial we shall show you how to swap RGB values in an Image. This is very useful when you are trying to create some effects to your Application.
  • Determine if an image has transparent pixels
    In this tutorial we shall show you how to check if an Image has transparent pixels. This example shows the basic techniques in order to gain very detailed information about your images.
  • Prevent an Application Window or Component from gaining focus
    With example we are going to see how to prevent a window and a graphical object from gaining focus in a Java Desktop Application.

AWT Tutorials – Event

Basic functionality examples based on AWT event

  • Window closing event handling
    In this example we are going to see how to handle window closing events. This is a very basic event handling when you are working on a UI Application.
  • Handle focus changes example
    In this tutorial we are going to see how to handle focus changed in a Java Desktop Application.
  • Determine click count example
    With this tutorial we are going to see how can you determine the number of clicks that a user performed over an object in a Java Desktop application.
  • Handle action events example
    In this tutorial we are going to see how ActionListener works in Java. This is one of the most important components you have to work with when you’re developing a GUI Application. The ActionListener is able to monitor a number of important events that occur in GUI Apps.
  • VetoableChangeListener example
    With this example you are going to learn about a very important event monitoring component, the VetoableChangeListener.
  • WindowListener demo
    In this tutorial we will see how to use the WindowListener. This is particularly useful when you are working with a window application and you want to monitor the state of each window.
  • Display all events occurring in a window
    In this example we shall show you how to display all events occurring in a windows. This is very useful when you want to have full control o events that occur in a Java applications that has many windows to handle. It is a very simple ways to manage the activity on your windows.
  • Multicast event example
    In this tutorial we are going to see how to manage multicast events. Multicast events are quite useful when you have to work with many windows in your application and you just want to perform the same action or a group of actions in a number of windows in your application at the same time.
  • Multiple listeners example
    In this example we are going to talk about how to work with multiple listeners in Java. This is particularly useful when you want to register more than one listeners in a single component, a button for instance.
  • Change Event behavior dynamically
    In this example we are going to see how to change the behavior of an event dynamically in your Java program.

AWT Tutorials – Listener

Basic functionality examples based on AWT listener

  • Items with the same ActionListener
    With this tutorial we are going to show you how to create several items with the same ActionListener. This is very useful when you want a number of components to behave in the same way under the occurrence of certain events.
  • Determine when a component moves becomes visible or resizes
    In this tutorial we are going to see how to use the ComponentAdapter class in Java. In some ways the ComponentAdapter is quite similar to the ComponentListener interface, but being a class it can be used more robustly, among some other features it implements.
  • Determine when a component is added or removed from a container
    In this example we shall show you how to use a ContainerListener in Java. When you develop an Application with dynamic GUI features, it’s very important to monitor the activities of the components that are added or removed from a component container, and that is the job of the ContainerListener.
  • Focus listener example
    This is an example that discusses how to use FocusListener in Java. This is a very handy feature when you have several components and you want to monitor and handle the event when on of them gains or looses focus.
  • PopupMenuListener example
    With this tutorial we shall show you how use PopupMenuListener interface in Java. You can use this listener to monitor your popup menus by overriding certain methods that fire up when a specific event concerning the popup menu occurs.
  • MenuListener example
    With this example we shall show you how the MenuListener interface works in Java. Menu components are very usual in rich, Graphical applications and they contribute to a better user experience.
  • JRadioButton with ItemListener example
    In this example we are going to see how to use JRadioButton bundled with an ItemListener. As you know, radio buttons a quite popular in Graphical Applications because they give flexibility to the end users, and it’s very natural to them to interact with radio buttons when you want to give specific values to choose from.
  • ListSelection example
    With this tutorial we shall show you how to perform List Selection activities using JList components and ListSelectionListener interface. List selection is a very useful feature, when you applications requires user input with fixed choices.
  • ItemListener example
    In this example we are going to see how to use several components like JRadioButton and JComboBox bundled with an ItemListener.
  • InternalFrameListener example
    In this example we are going to see how to use InternalFrameListener in Java. When you are developing an application with a vivid Graphical environment it is very possible that you will use JInternalFrame components to enhance the end user’s experience.
  • HyperLinkListener example
    With this example we shall show you how to use a HyperLinkListener in Java. This is very useful when you have hyperlinks in the content you present in your application. In that case it might be important to monitor the hyperlink activity of the applications.
  • FocusListener example
    This is an example that discusses how to use FocusListener in Java. This is a very handy feature when you have several components and you want to monitor and handle the event when on of them gains or looses focus.
  • ContainerListener example
    In this example we shall show you how to use a ContainerListener in Java. When you develop an Application with dynamic GUI features it’s very important to monitor the activities of the components that are added or removed from a component container, and that is the job of the ContainerListener.
  • Maximization event example
    With this tutorial we shall show you how use the ComponentListener interface to handle Maximization events in your Java Application.
  • Print window position example
    In this example we are going to see how to use a ComponentListener in order to print the window position in a Java application. The position of the window will be updated every time the user changes position to the window.
  • ComponentAdapter example
    In this tutorial we are going to see how to use the ComponentAdapter class in Java. In some ways the ComponentAdapter is quite similar to the ComponentListener interface, but being a class it can be used more robustly, among some other features it implements.
  • ComponentListener example
    With this example we are going to see how to use a ComponentListener in order to monitor the window events in our Java Application. This may be useful when you want your application to react differently depending on the position or the kind of movement of the window.
  • AncestorListener example with timer
    In this example we are going to see how AncestorListener can be paired with a timer in Java. This is very useful when you add or remove components to your Java Application and you want to monitor the relationship changes that follow these actions.
  • AdjustmentListener example
    In this tutorial we are going to see how AdjustmentListener works in Java. It is quite useful when you want to monitor a very wide variety of changes in a program with rich GUI components.
  • Java ActionListener example
    In this tutorial we are going to see how ActionListener works in Java. This is one the most important components you have to work with when you’re developing a GUI Application.
  • WindowListener example
    With this tutorial we shall show you how to use the WindowListener interface in Java. This component can be very useful when you develop an application with many windows an many frames, and you mast have full control over window changes.
  • Action Class example
    With this example we shall show you how to work with Action class in Java. In many ways the use of the Action class is pretty much the same as the use of the ActionListener interface.
  • A simple application to Load – Save – Edit a file
    This is an example on how to create a simple GUI application that Loads, Edits and Saves a file in Java. You might find it useful if you wish to add such a functionality built into your app.
  • List actions of some Components
    With this example we shall show you how to create a simple application that lists the actions of some Components you can use in your Java Desktop Application.
  • Focus change example
    In this example we are going to see how to change focus in a Java Application.
  • A simple Timer example
    With this tutorial we are going to see how to use the Timer component in Java. The Timer component is very useful when you want to schedule some tasks in your application.
  • PropertyChangeListener example
    In this tutorial we are going to see how to use PropertyChangeListener in Java. This is very important when you are working with many components and you want to have full control over their properties.
  • AncestorListener example
    In this example we are going to see how AncestorListener can be paired with a timer in Java. This is very useful when you add or remove components to your Java Application and you want to monitor the relationship changes that follow these actions.
  • A simple ChangeListener example
    In this example we are going to see how to use the ChangeListener interface in Java. This is very useful when you want to monitor generic changes in your application.
  • A simple text menu using TextAction example
    With this tutorial we are going to show you how to create an application with a simple text menu that uses TextAction in Java. This will be particularly useful when you want to create a simple and fast menu for your GUI Application.
  • CheckBox Listener example
    In this tutorial we are going to see how you can use an ActionListener, a ChangeListener and an ItemListener with the JCheckBox component to construct a CheckBox listener.
  • ChangeListener example
    In this example we are going to see how to use the ChangeListener interface in Java. This is very useful when you want to monitor generic changes in your application.
  • Display all the occurring events
    In this tutorial we are going to create a simple application that displays all the occurring events that might take place in a Java Desktop Application.
  • Multilistener example
    In this tutorial we shall show you how to use an ActionListener in order to create a Multilistener component in a Java Desktop application.

AWT Tutorials – Mouse Functions

Basic examples based on AWT mouse functions

  • Mouse button detection
    With this example we shall show you how to use a MouseAdapter in order to detect which button of the mouse the user used. This is very useful when you want to add some extra functionality or flexibility to your application. It might be useful to the user to give input to the program using his mouse.
  • MouseWheelListener example
    With this tutorial we are going to show you how to use a MouseWheelListener in Java. This event listener is particularly useful when you want to add extra user friendly functionality in your application.
  • MouseListener example
    In this example we are going to see how you can use the MouseListener interface in Java, in order to monitor the mouse activity on your application.
  • Handling Mouse Clicks
    In this short tutorial we are going to see how to monitor a very basic GUI event in Java Desktop Applications, which is mouse clicks. What we want to see is what button mouse the user pressed (left, middle or right click) and then we want to know the exact location, in coordinates, in our Frame that this event occurred.
  • Handling Mouse Clicks example
    In this short tutorial we are going to see how to monitor a very basic GUI event in Java Desktop Applications, which is mouse clicks. What we want to see is what button mouse the user pressed and then we want to know the exact location, in coordinates, in our Frame that this event occurred.
  • Handle mouse clicks example
    In this short tutorial we are going to see how to monitor a very basic GUI event in Java Desktop Applications, which is mouse clicks. What we want to see is what button mouse the user pressed and then we want to know the exact location, in coordinates, in our Frame that this event occurred.
  • Detect double or triple click example
    With this tutorial we are going to see how can you detect double or triple clicks in a Java Desktop application. This is very useful when you want the user to have rich interaction with your application using his mouse.
  • Move and resize objects using Mouse Events
    With this tutorial we are going to see how to handle moving and re sizing events in a Java Desktop application. You might find this useful when you want to add rich mouse functionality to the application.
  • MouseMotionListener example
    With this tutorial we shall show you how to work with the MouseMotionListener interface in Java. It is a very useful feature when you want to have full control over mouse events and mouse input that the users give.
  • Mouse motion listener example
    With this tutorial we shall show you how to work with the MouseMotionListener interface in Java. It is a very useful feature when you want to have full control over mouse events and mouse input that the users give.
  • A simple mouse drag example
    With this example we shall show you how to work with MouseListener and MouseMotionListener interfaces in order to handle and monitor mouse events an especially mouse drags.
  • MouseMotion event example
    With this tutorial we shall show you how to work with the MouseMotionListener interface in Java. It is a very useful feature when you want to have full control over mouse events and mouse input that the users give.
  • Drag event example
    With this example we shall show you how to work with MouseListener and MouseMotionListener interfaces in order to handle and monitor mouse events an especially mouse drags.
  • Mouse wheel event example
    With this tutorial we are going to sow you how to use a MouseWheelListener in Java. This event listener is particularly useful when you want to add extra user friendly functionality in your application.
  • Drag and Selection example
    With this example we shall show you how to work with MouseListener and MouseMotionListener interfaces in order to handle and monitor mouse events an especially mouse drags.
  • Simulate mouse move and key pressing
    In this example we are going to see how to simulate mouse moves and key pressing events in a Java Desktop Application. This is a quite cool feature that you can use in many ways in your application.
  • Changing cursor shape when hovering over components
    In this example we are going to see how to change the shape of the cursor when hovering over components. This will create an impressive and practical graphic for your application, which will notify the users that they are pointing to an object with some certain functionality.

AWT Tutorials – Keys

Basic examples based on AWT key functionality

  • Simple key press listener
    With this simple tutorial we are going to see how to implement a simple key listener for your Java Desktop Application. This is a very nice tool if your application offers rich keyboard activity to the user.
  • Key event listener to capitalize letter keys example
    In this example we shall show you how to use KeyEventDispatcher interfacce in order to create a simple application that capitalizes key letters on the fly in a Java Desktop Application.
  • Change active keystrokes
    With this tutorial we shall show you how to change active keystrokes in a Java application. You might find this useful when you are developing an application that has rich and intense keyboard activity and you want to customize it to make it very flexible for each user dynamically to use his own keystrokes.
  • A complete KeyListener example
    In this example we are going to see how you can use a KeyListener in Java, in order to monitor key events that take place in your applications.
  • Keymap and KeyStroke example
    With this example we are going to see how to use Keymap and KeyStroke classes in a Java. These classes are useful when you want to bind a certain key stroke with the default button of the window.
  • KeyStrokes example
    In this example we are going to see how to work with key strokes in a Java Desktop Application. You might find this useful when you want to bind specific keystrokes with certain behaviors in your app.
  • List Key strokes example
    In this example we are going to see how to list all Key Strokes in Java. This is very useful when you want to inform users of the application or the client code about all available key strokes.
  • Activate a Keystroke when the window has focus
    With this tutorial we shall show you how to activate a Keystroke when on of the window of out application gains focus.
  • GetFocusTraversalKeys example
    In this example we are going to see how to get the focus traversal keys of an input device in a Java Desktop Application.
  • A complete Key Event example
    With example we shall show you how to create a simple application that can handle all key events. This is very useful when your application gets lots of keyboard input form the user.
  • Arrow sketch on JFrame example
    With example we shall show you how to create a simple application with JFrame.
  • KeyListener example
    In this example we are going to see how you can use a KeyListener in Java, in order to monitor key events that take place in your applications.
  • Handle key presses example
    With this simple tutorial we are going to see how to implement a simple key listener for your Java Desktop Application. This is a very nice tool if your application offers rich keyboard activity to the user.

[undereg]

Back to top button