Skip to main content

Overview

The java.desktop module defines the AWT and Swing user interface toolkits, plus APIs for accessibility, audio, imaging, printing, and JavaBeans. This module has been part of the Java Platform since Java SE 9 as a modularized component of the desktop APIs.

Module Declaration

module java.desktop {
    requires java.prefs;
    requires transitive java.datatransfer;
    requires transitive java.xml;
    
    exports java.awt;
    exports java.awt.color;
    exports java.awt.desktop;
    exports java.awt.dnd;
    exports java.awt.event;
    exports java.awt.font;
    exports java.awt.geom;
    exports java.awt.im;
    exports java.awt.image;
    exports java.awt.print;
    exports java.beans;
    exports javax.swing;
    exports javax.swing.border;
    exports javax.swing.event;
    exports javax.swing.table;
    exports javax.swing.tree;
    // ... and more
}

Key Components

The java.desktop module provides comprehensive desktop application development capabilities through several major API groups:

AWT (Abstract Window Toolkit)

Low-level windowing toolkit with graphics, events, and native components

Swing

Lightweight GUI framework built on top of AWT with rich components

Java 2D

Advanced 2D graphics, imaging, and printing capabilities

Image I/O

Reading and writing images in various formats

Accessibility

APIs for assistive technologies and accessible applications

JavaBeans

Component architecture for reusable software components

Sound

Audio playback and MIDI support via javax.sound packages

Printing

Print service discovery and document printing APIs

Core Packages

java.awt

Core AWT classes including:
  • Component: Base class for all AWT components
  • Container: Components that can contain other components
  • Frame: Top-level window with title and border
  • Graphics/Graphics2D: Drawing and rendering context
  • LayoutManager: Interface for component layout

java.awt.event

Event handling classes:
  • ActionListener: Handles action events from buttons, menus
  • MouseListener: Mouse click and press events
  • KeyListener: Keyboard input events
  • WindowListener: Window state change events

java.awt.geom

2D geometry classes:
  • Point2D, Line2D, Rectangle2D: Basic shapes
  • AffineTransform: Coordinate transformations
  • Path2D: Complex path definitions

Module Dependencies

The java.desktop module has the following dependencies:
java.prefs
required
Preferences API for storing user and system configuration data
java.datatransfer
required transitive
Clipboard and drag-and-drop data transfer mechanisms
java.xml
required transitive
XML processing APIs used by JavaBeans persistence

Service Provider Interfaces

The module defines several service provider interfaces for extensibility:
uses java.awt.im.spi.InputMethodDescriptor;
uses javax.accessibility.AccessibilityProvider;
uses javax.imageio.spi.ImageReaderSpi;
uses javax.imageio.spi.ImageWriterSpi;
uses javax.print.PrintServiceLookup;
uses javax.sound.midi.spi.MidiDeviceProvider;
uses javax.sound.sampled.spi.AudioFileReader;

Usage Example

Basic desktop application using java.desktop APIs:
import java.awt.*;
import javax.swing.*;

public class DesktopExample {
    public static void main(String[] args) {
        // Create application window
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Desktop API Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
            
            // Create panel with components
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JLabel("Hello from java.desktop!"), 
                     BorderLayout.NORTH);
            
            JButton button = new JButton("Click Me");
            button.addActionListener(e -> 
                JOptionPane.showMessageDialog(frame, "Button clicked!"));
            panel.add(button, BorderLayout.CENTER);
            
            frame.setContentPane(panel);
            frame.setVisible(true);
        });
    }
}

Threading Considerations

Swing is not thread-safe. All GUI operations must be performed on the Event Dispatch Thread (EDT). Use SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait() to execute code on the EDT from other threads.
// Correct: Update UI from background thread
new Thread(() -> {
    // Perform background work
    String result = doBackgroundWork();
    
    // Update UI on EDT
    SwingUtilities.invokeLater(() -> {
        label.setText(result);
    });
}).start();

Platform Integration

The module provides platform-specific integrations:
  • Desktop class: Integration with native desktop features (open files, browse URLs)
  • SystemTray: Access to system tray/notification area
  • Taskbar: Platform-specific taskbar operations
  • SplashScreen: Native splash screen support

See Also

Build docs developers (and LLMs) love