How do I create a window using JFrame in Java?

Use JFrame to create a Swing window with a title bar and an area for components. In this example, you will open a window with a centered welcome message.

Source code

Save the following code as JFrameExample.java:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class JFrameExample {
    void main() {
        SwingUtilities.invokeLater(this::showWindow);
    }

    private void showWindow() {
        JFrame frame = new JFrame("Hello, Swing!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel message = new JLabel(
                "Welcome to Swing", SwingConstants.CENTER);
        frame.add(message);

        frame.setSize(400, 240);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

What the window looks like

Hello Swing!

This illustration shows the layout. The title bar and window controls may look different on your operating system.

Explanation

new JFrame("Hello, Swing!") creates the window and sets its title. EXIT_ON_CLOSE tells Java to exit the application when you close this window.

The JLabel displays Welcome to Swing. SwingConstants.CENTER centers the text horizontally, and frame.add(message) places the label inside the window.

setSize(400, 240) sets the size of the entire window, including its border and title bar. Call setLocationRelativeTo(null) after setting the size to center the window on the screen. Finally, setVisible(true) displays it.

The main() method uses SwingUtilities.invokeLater(this::showWindow) to schedule window creation on Swing’s event dispatch thread. This is the thread Swing uses to handle events and update the interface.

Run the example

Compile and run the file:

javac JFrameExample.java
java JFrameExample

A window titled Hello, Swing! opens with the welcome message in the center. Close the window to end the program.

How do I create, load, and display an image in Java?

Java Image Tutorials

In this example, you will draw a blue card in memory, save it as a PNG file, open that file, and show the loaded image in a Swing window. The program creates its own image.

What is a pixel?

A raster image is a grid of pixels. In a BufferedImage, the top-left pixel is at (0, 0). The x coordinate increases to the right, and the y coordinate increases downward.

This example uses BufferedImage.TYPE_INT_ARGB. Each pixel has four values for alpha, red, green, and blue. Alpha controls opacity. A value of 0 means fully transparent, and 255 means fully opaque. The program draws inside the image and leaves a transparent border, which PNG preserves when you save the file.

Java example

Save the following code as JavaImageBasics.java.

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class JavaImageBasics {
    public static void main(String[] args) throws IOException {
        BufferedImage image = createImage();

        File file = new File("java-image.png");
        if (!ImageIO.write(image, "png", file)) {
            throw new IOException("PNG writer is unavailable");
        }

        BufferedImage loadedImage = ImageIO.read(file);
        if (loadedImage == null) {
            throw new IOException("Unrecognized image format: " + file);
        }

        System.out.printf("%s: %d x %d pixels%n",
                file.getName(), loadedImage.getWidth(), loadedImage.getHeight());

        if (!GraphicsEnvironment.isHeadless()) {
            SwingUtilities.invokeLater(() -> showImage(loadedImage));
        }
    }

    private static BufferedImage createImage() {
        BufferedImage image = new BufferedImage(
                320, 200, BufferedImage.TYPE_INT_ARGB);

        Graphics2D graphics = image.createGraphics();
        try {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setColor(new Color(33, 99, 200));
            graphics.fillRoundRect(20, 20, 280, 160, 30, 30);
            graphics.setColor(Color.WHITE);
            graphics.drawString("Hello, Java Images!", 100, 105);
        } finally {
            graphics.dispose();
        }

        return image;
    }

    private static void showImage(BufferedImage image) {
        JFrame frame = new JFrame("Java Images");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JLabel(new ImageIcon(image)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Run the program

Open a terminal in the folder containing the Java file and run these commands.

javac JavaImageBasics.java
java JavaImageBasics

The program writes java-image.png to the working directory and prints the dimensions of the loaded file.

Java Image

java-image.png: 320 x 200 pixels

On a desktop, a Swing window shows the blue card. The transparent border lets the component background show through. In a headless environment, the program still writes and reads the file and prints its dimensions.

How it works

  1. Create. createGraphics() returns a Graphics2D object that draws directly into the BufferedImage. The finally block calls dispose() to release the drawing context.
  2. Save. ImageIO.write() selects an encoder using the format name "png". It returns false if no suitable writer is available.
  3. Load. ImageIO.read() decodes the file into a new BufferedImage. It returns null if no installed reader recognizes the data. An error while opening or reading the file can throw an IOException.
  4. Display. ImageIcon wraps the loaded image, and JLabel displays the icon. SwingUtilities.invokeLater() creates the window on Swing’s Event Dispatch Thread after the file has been read.

Try it yourself

Change the image size to 480 × 240 pixels. Resize the blue rectangle so it still has a 20-pixel margin on all sides. Then print the alpha value for the top-left pixel and for a pixel inside the blue rectangle.

int cornerAlpha = loadedImage.getRGB(0, 0) >>> 24;
int cardAlpha = loadedImage.getRGB(30, 50) >>> 24;
System.out.printf("Corner alpha = %d, card alpha = %d%n",
        cornerAlpha, cardAlpha);

Add this snippet after the null check in main(). The expected values are 0 for the transparent corner and 255 for the solid blue area.

Maven dependencies

AWT, Swing, and Image I/O are part of the JDK’s java.desktop module. This example needs no extra Maven dependencies. If you use a modular project, add requires java.desktop; to your module declaration.

References

Next — How do I choose an image file format in Java?

How do I build an analog world clock widget in Java Swing?

You can build a desktop world clock in Java Swing by converting one Instant into several time zones and drawing each clock face with Java 2D. This example displays an analog dial, a digital time, the local date, and the UTC offset for each city. The widget can stay above other windows while you work.

Java Swing world clock widget with analog dials for Denpasar, Jakarta, Tokyo, London, and New York

Source code

Save the source as WorldClock.java. The example uses Java 17 or later and runs with the standard JDK.

package org.kodejava.swing;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.prefs.Preferences;

public final class WorldClock {
    private static final Color BACKGROUND = new Color(13, 21, 35);
    private static final Color CARD = new Color(24, 36, 54);
    private static final Color TEXT = new Color(240, 245, 250);
    private static final Color MUTED = new Color(166, 184, 205);
    private static final Color[] ACCENTS = {
            new Color(121, 224, 196), new Color(247, 190, 118),
            new Color(165, 186, 255), new Color(250, 158, 175)
    };
    private static final DateTimeFormatter DATE =
            DateTimeFormatter.ofPattern("EEE, d MMM", Locale.ENGLISH);
    private static final DateTimeFormatter[] TIME_FORMATS = {
            DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT),
            DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ROOT),
            DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH),
            DateTimeFormatter.ofPattern("h:mm:ss a", Locale.ENGLISH)
    };
    private static final String[] CITY_DATA = {
            "Amsterdam|Europe/Amsterdam", "Auckland|Pacific/Auckland", "Bangkok|Asia/Bangkok",
            "Beijing|Asia/Shanghai", "Berlin|Europe/Berlin", "Denpasar|Asia/Makassar",
            "Dubai|Asia/Dubai", "Hong Kong|Asia/Hong_Kong", "Honolulu|Pacific/Honolulu",
            "Istanbul|Europe/Istanbul", "Jakarta|Asia/Jakarta", "Johannesburg|Africa/Johannesburg",
            "Kuala Lumpur|Asia/Kuala_Lumpur", "London|Europe/London",
            "Los Angeles|America/Los_Angeles", "Manila|Asia/Manila",
            "Mexico City|America/Mexico_City", "Moscow|Europe/Moscow", "New Delhi|Asia/Kolkata",
            "New York|America/New_York", "Paris|Europe/Paris", "Perth|Australia/Perth",
            "São Paulo|America/Sao_Paulo", "Seoul|Asia/Seoul", "Singapore|Asia/Singapore",
            "Sydney|Australia/Sydney", "Tokyo|Asia/Tokyo", "Toronto|America/Toronto",
            "UTC|UTC", "Vancouver|America/Vancouver"
    };
    private static final String DEFAULTS = "Denpasar|Jakarta|Tokyo|London|New York";
    private static final List<City> CATALOG = java.util.Arrays.stream(CITY_DATA)
            .map(entry -> entry.split("\\|"))
            .map(parts -> new City(parts[0], ZoneId.of(parts[1])))
            .toList();

    private record City(String name, ZoneId zone) {
        @Override
        public String toString() {
            return name;
        }
    }

    private final JFrame frame = new JFrame("World Clock");
    private final JPanel content = new JPanel(new BorderLayout(0, 10));
    private final JPanel list = new JPanel();
    private final JScrollPane scroll = new JScrollPane(list);
    private final List<City> cities = new ArrayList<>();
    private final List<CityCard> cards = new ArrayList<>();
    private final Preferences settings;
    private final Timer timer = new Timer(50, event -> refreshClocks());
    private final JLabel empty = label("Use + City to add a clock.", 13, MUTED);
    private TrayIcon trayIcon;
    private boolean seconds;
    private boolean twelveHour;
    private boolean compact;

    private WorldClock(boolean preview) {
        settings = preview ? null : Preferences.userNodeForPackage(WorldClock.class)
                .node("world-clock-swing");
        boolean pinned = settings == null || settings.getBoolean("pinned", true);
        seconds = settings == null || settings.getBoolean("seconds", true);
        twelveHour = settings != null && settings.getBoolean("twelveHour", false);
        compact = settings != null && settings.getBoolean("compact", false);
        String names = settings == null ? DEFAULTS : settings.get("cities", DEFAULTS);
        for (String name : names.split("\\|")) {
            CATALOG.stream().filter(city -> city.name().equals(name) && !cities.contains(city))
                    .findFirst().ifPresent(cities::add);
        }

        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        frame.setAlwaysOnTop(pinned);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                hideWidget();
            }
        });
        content.setBackground(BACKGROUND);
        content.setBorder(new EmptyBorder(10, 12, 12, 12));
        frame.setContentPane(content);

        JPanel header = new JPanel(new BorderLayout(8, 0));
        header.setOpaque(false);
        JLabel title = label("World Clock", 20, TEXT);
        title.setFont(title.getFont().deriveFont(Font.BOLD));
        header.add(title, BorderLayout.CENTER);
        JPanel actions = new JPanel(new FlowLayout(FlowLayout.RIGHT, 4, 0));
        actions.setOpaque(false);
        JButton add = button("+ City");
        JButton options = button("Options");
        JButton hide = button("−");
        hide.setToolTipText("Hide widget");
        hide.getAccessibleContext().setAccessibleName("Hide widget");
        add.addActionListener(event -> addCity());
        options.addActionListener(event -> showOptions(options));
        hide.addActionListener(event -> hideWidget());
        actions.add(add);
        actions.add(options);
        actions.add(hide);
        header.add(actions, BorderLayout.EAST);
        enableDrag(title);
        enableDrag(header);
        content.add(header, BorderLayout.NORTH);

        list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS));
        list.setBackground(BACKGROUND);
        scroll.setBorder(null);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.getVerticalScrollBar().setUnitIncrement(24);
        content.add(scroll, BorderLayout.CENTER);
        frame.getRootPane().registerKeyboardAction(event -> hideWidget(),
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

        Rectangle area = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        frame.setBounds(area.x + area.width - 384, area.y + 24, 360, 200);
        if (settings != null) {
            frame.setLocation(settings.getInt("x", frame.getX()), settings.getInt("y", frame.getY()));
        }
        rebuildCards();
        if (!preview) installTray();
    }

    private static JLabel label(String text, int size, Color color) {
        JLabel label = new JLabel(text);
        label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, size));
        label.setForeground(color);
        return label;
    }

    private static JButton button(String text) {
        JButton button = new JButton(text);
        button.setBackground(CARD);
        button.setForeground(TEXT);
        button.setMargin(new Insets(4, 8, 4, 8));
        return button;
    }

    private void enableDrag(Component target) {
        MouseAdapter drag = new MouseAdapter() {
            private Point offset;

            @Override
            public void mousePressed(MouseEvent event) {
                if (!SwingUtilities.isLeftMouseButton(event)) return;
                Point pointer = event.getLocationOnScreen();
                offset = new Point(pointer.x - frame.getX(), pointer.y - frame.getY());
            }

            @Override
            public void mouseDragged(MouseEvent event) {
                if (offset == null) return;
                Point pointer = event.getLocationOnScreen();
                frame.setLocation(pointer.x - offset.x, pointer.y - offset.y);
            }

            @Override
            public void mouseReleased(MouseEvent event) {
                if (offset == null) return;
                offset = null;
                fitToScreen();
                save();
            }
        };
        target.addMouseListener(drag);
        target.addMouseMotionListener(drag);
    }

    private void fitToScreen() {
        GraphicsConfiguration screen = frame.getGraphicsConfiguration();
        Rectangle area = new Rectangle(screen.getBounds());
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(screen);
        area.x += insets.left;
        area.y += insets.top;
        area.width -= insets.left + insets.right;
        area.height -= insets.top + insets.bottom;
        Dimension preferred = content.getPreferredSize();
        int width = Math.min(area.width, 360);
        int height = Math.min(area.height, preferred.height);
        int x = Math.max(area.x, Math.min(frame.getX(), area.x + area.width - width));
        int y = Math.max(area.y, Math.min(frame.getY(), area.y + area.height - height));
        frame.setBounds(x, y, width, height);
    }

    private void addCity() {
        City[] available = CATALOG.stream().filter(city -> !cities.contains(city)).toArray(City[]::new);
        if (available.length == 0) {
            JOptionPane.showMessageDialog(frame, "All cities are already displayed.");
            return;
        }
        City choice = (City) JOptionPane.showInputDialog(frame, "Choose a city or time zone",
                "Add City", JOptionPane.PLAIN_MESSAGE, null, available, available[0]);
        if (choice == null) return;
        cities.add(choice);
        rebuildCards();
        save();
        CityCard added = cards.get(cards.size() - 1);
        SwingUtilities.invokeLater(() -> list.scrollRectToVisible(added.getBounds()));
    }

    private void showOptions(JButton anchor) {
        JPopupMenu menu = new JPopupMenu();
        toggle(menu, "Always on top", frame.isAlwaysOnTop(), frame::setAlwaysOnTop);
        toggle(menu, "Show seconds", seconds, value -> seconds = value);
        toggle(menu, "12-hour format", twelveHour, value -> twelveHour = value);
        toggle(menu, "Compact layout", compact, value -> {
            compact = value;
            rebuildCards();
        });
        menu.addSeparator();
        item(menu, "Exit", true, this::exit);
        menu.show(anchor, 0, anchor.getHeight());
    }

    private void toggle(JPopupMenu menu, String title, boolean checked,
                        java.util.function.Consumer<Boolean> change) {
        JCheckBoxMenuItem item = new JCheckBoxMenuItem(title, checked);
        item.addActionListener(event -> {
            change.accept(item.isSelected());
            refreshClocks();
            save();
        });
        menu.add(item);
    }

    private static void item(JPopupMenu menu, String title, boolean enabled, Runnable action) {
        JMenuItem item = new JMenuItem(title);
        item.setEnabled(enabled);
        item.addActionListener(event -> action.run());
        menu.add(item);
    }

    private void showCityMenu(JButton anchor, City city) {
        int index = cities.indexOf(city);
        JPopupMenu menu = new JPopupMenu();
        item(menu, "Move up", index > 0, () -> moveCity(index, index - 1));
        item(menu, "Move down", index < cities.size() - 1, () -> moveCity(index, index + 1));
        item(menu, "Remove", true, () -> {
            cities.remove(city);
            rebuildCards();
            save();
        });
        menu.show(anchor, 0, anchor.getHeight());
    }

    private void moveCity(int index, int destination) {
        City city = cities.remove(index);
        cities.add(destination, city);
        rebuildCards();
        save();
        CityCard moved = cards.get(destination);
        SwingUtilities.invokeLater(() -> list.scrollRectToVisible(moved.getBounds()));
    }

    private void rebuildCards() {
        list.removeAll();
        cards.clear();
        if (cities.isEmpty()) {
            empty.setBorder(new EmptyBorder(12, 8, 12, 8));
            list.add(empty);
        }
        for (int index = 0; index < cities.size(); index++) {
            if (index > 0) list.add(Box.createVerticalStrut(6));
            City city = cities.get(index);
            Color accent = ACCENTS[CATALOG.indexOf(city) % ACCENTS.length];
            CityCard card = new CityCard(city, accent);
            cards.add(card);
            list.add(card);
        }
        list.revalidate();
        fitToScreen();
        list.repaint();
        refreshClocks();
    }

    private void refreshClocks() {
        Instant now = Instant.now();
        LocalDate localDate = now.atZone(ZoneId.systemDefault()).toLocalDate();
        int formatIndex = (twelveHour ? 2 : 0) + (seconds ? 1 : 0);
        for (CityCard card : cards) {
            ZonedDateTime cityTime = now.atZone(card.city.zone());
            card.refresh(cityTime, localDate, TIME_FORMATS[formatIndex]);
        }
    }

    private void save() {
        if (settings == null) return;
        settings.put("cities", String.join("|", cities.stream().map(City::name).toList()));
        settings.putInt("x", frame.getX());
        settings.putInt("y", frame.getY());
        settings.putBoolean("pinned", frame.isAlwaysOnTop());
        settings.putBoolean("seconds", seconds);
        settings.putBoolean("twelveHour", twelveHour);
        settings.putBoolean("compact", compact);
    }

    private void installTray() {
        if (!SystemTray.isSupported()) return;
        BufferedImage icon = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = icon.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(CARD);
            g.fillOval(0, 0, 32, 32);
            g.setColor(ACCENTS[0]);
            g.setStroke(new BasicStroke(2.4f));
            g.drawOval(3, 3, 26, 26);
            g.drawLine(16, 16, 16, 7);
            g.drawLine(16, 16, 23, 20);
        } finally {
            g.dispose();
        }
        PopupMenu menu = new PopupMenu();
        MenuItem show = new MenuItem("Show World Clock");
        show.addActionListener(event -> SwingUtilities.invokeLater(this::showWidget));
        menu.add(show);
        MenuItem close = new MenuItem("Exit");
        close.addActionListener(event -> SwingUtilities.invokeLater(this::exit));
        menu.add(close);
        TrayIcon iconCandidate = new TrayIcon(icon, "World Clock", menu);
        iconCandidate.setImageAutoSize(true);
        iconCandidate.addActionListener(event -> SwingUtilities.invokeLater(this::showWidget));
        try {
            SystemTray.getSystemTray().add(iconCandidate);
            trayIcon = iconCandidate;
        } catch (AWTException error) {
            // The taskbar remains available if a tray icon cannot be registered.
        }
    }

    private void showWidget() {
        fitToScreen();
        frame.setExtendedState(JFrame.NORMAL);
        frame.setVisible(true);
        frame.toFront();
        refreshClocks();
        timer.start();
    }

    private void hideWidget() {
        if (trayIcon != null) {
            timer.stop();
            frame.setVisible(false);
        } else {
            frame.setExtendedState(JFrame.ICONIFIED);
        }
    }

    private void exit() {
        timer.stop();
        save();
        if (trayIcon != null) SystemTray.getSystemTray().remove(trayIcon);
        frame.dispose();
    }

    private final class CityCard extends JPanel {
        private final City city;
        private final Dial dial;
        private final JLabel digital = label("", compact ? 17 : 21, TEXT);
        private final JLabel date = label("", 12, MUTED);

        CityCard(City city, Color accent) {
            super(new BorderLayout(10, 0));
            this.city = city;
            setOpaque(false);
            setBorder(new EmptyBorder(6, 9, 6, 8));
            int height = compact ? 70 : 104;
            setPreferredSize(new Dimension(320, height));
            setMaximumSize(new Dimension(Integer.MAX_VALUE, height));
            setAlignmentX(Component.LEFT_ALIGNMENT);
            dial = new Dial(accent, height - 12);
            add(dial, BorderLayout.WEST);

            JPanel info = new JPanel(new GridBagLayout());
            info.setOpaque(false);
            JPanel text = new JPanel();
            text.setOpaque(false);
            text.setLayout(new BoxLayout(text, BoxLayout.Y_AXIS));
            JLabel name = label(city.name(), 13, accent);
            name.setFont(name.getFont().deriveFont(Font.BOLD));
            digital.setFont(new Font(Font.MONOSPACED, Font.BOLD, compact ? 17 : 21));
            text.add(name);
            text.add(digital);
            text.add(date);
            GridBagConstraints layout = new GridBagConstraints();
            layout.weightx = 1;
            layout.fill = GridBagConstraints.HORIZONTAL;
            info.add(text, layout);
            add(info, BorderLayout.CENTER);
            JButton actions = button("⋯");
            actions.setMargin(new Insets(2, 5, 2, 5));
            actions.getAccessibleContext().setAccessibleName("Options for " + city.name());
            actions.addActionListener(event -> showCityMenu(actions, city));
            JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 2));
            actionPanel.setOpaque(false);
            actionPanel.add(actions);
            add(actionPanel, BorderLayout.EAST);
        }

        void refresh(ZonedDateTime time, LocalDate localDate, DateTimeFormatter format) {
            long difference = ChronoUnit.DAYS.between(localDate, time.toLocalDate());
            String day = difference == 0 ? "" : String.format(Locale.ROOT, "  ·  %+dd", difference);
            String offset = time.getOffset().equals(ZoneOffset.UTC) ? "+00:00" : time.getOffset().getId();
            digital.setText(time.format(format));
            date.setText(time.format(DATE) + "  ·  UTC" + offset + day);
            dial.update(time, seconds);
        }

        @Override
        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            Graphics2D g = (Graphics2D) graphics.create();
            try {
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(CARD);
                g.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
            } finally {
                g.dispose();
            }
        }
    }

    private static final class Dial extends JComponent {
        private final Color accent;
        private ZonedDateTime time;
        private boolean showSeconds;

        Dial(Color accent, int size) {
            this.accent = accent;
            setPreferredSize(new Dimension(size, size));
        }

        void update(ZonedDateTime nextTime, boolean seconds) {
            boolean minuteChanged = time == null ||
                                    !time.truncatedTo(ChronoUnit.MINUTES).equals(nextTime.truncatedTo(ChronoUnit.MINUTES));
            boolean repaintNeeded = seconds || minuteChanged || showSeconds != seconds;
            time = nextTime;
            showSeconds = seconds;
            if (repaintNeeded) repaint();
        }

        @Override
        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            if (time == null) return;
            Graphics2D g = (Graphics2D) graphics.create();
            try {
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                double size = Math.min(getWidth(), getHeight());
                g.translate(getWidth() / 2.0, getHeight() / 2.0);
                g.scale(size / 100.0, size / 100.0);

                g.setPaint(new GradientPaint(-35, -48, new Color(49, 70, 93),
                        30, 49, new Color(11, 19, 33)));
                g.fill(new Ellipse2D.Double(-48, -48, 96, 96));
                g.setColor(accent);
                g.setStroke(new BasicStroke(1.6f));
                g.draw(new Ellipse2D.Double(-46, -46, 92, 92));
                g.setPaint(new RadialGradientPaint(new Point2D.Double(-13, -17), 65,
                        new float[]{0, 1}, new Color[]{new Color(42, 59, 79), new Color(9, 16, 29)}));
                g.fill(new Ellipse2D.Double(-43, -43, 86, 86));

                for (int tick = 0; tick < 60; tick++) {
                    boolean hour = tick % 5 == 0;
                    double angle = tick * Math.PI / 30;
                    g.setColor(hour ? accent : new Color(102, 124, 149));
                    g.setStroke(new BasicStroke(hour ? 1.7f : 0.7f, BasicStroke.CAP_ROUND,
                            BasicStroke.JOIN_ROUND));
                    g.draw(radialLine(angle, hour ? 35 : 38, 40));
                }

                g.setColor(TEXT);
                g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 9));
                numeral(g, "12", 0, -27);
                numeral(g, "3", 27, 0);
                numeral(g, "6", 0, 27);
                numeral(g, "9", -27, 0);

                double second = showSeconds ? time.getSecond() + time.getNano() / 1_000_000_000.0 : 0;
                double minute = time.getMinute() + second / 60;
                double hour = time.getHour() % 12 + minute / 60;
                hand(g, hour / 12, 23, 4.3f, TEXT);
                hand(g, minute / 60, 33, 2.9f, TEXT);
                if (showSeconds) hand(g, second / 60, 36, 1.4f, accent);
                g.setColor(accent);
                g.fill(new Ellipse2D.Double(-3.5, -3.5, 7, 7));
                g.setColor(BACKGROUND);
                g.fill(new Ellipse2D.Double(-1.3, -1.3, 2.6, 2.6));
            } finally {
                g.dispose();
            }
        }

        private static void numeral(Graphics2D g, String text, int x, int y) {
            FontMetrics metrics = g.getFontMetrics();
            g.drawString(text, x - metrics.stringWidth(text) / 2.0f,
                    y + (metrics.getAscent() - metrics.getDescent()) / 2.0f);
        }

        private static void hand(Graphics2D g, double turn, double length, float width, Color color) {
            g.setColor(color);
            g.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g.draw(radialLine(turn * 2 * Math.PI, -5, length));
        }

        private static Line2D radialLine(double angle, double start, double end) {
            double x = Math.sin(angle);
            double y = -Math.cos(angle);
            return new Line2D.Double(x * start, y * start, x * end, y * end);
        }
    }

    private static void verifyAndRender(Path output) throws IOException {
        Instant winter = Instant.parse("2026-01-15T19:30:00Z");
        Instant summer = Instant.parse("2026-07-15T19:30:00Z");
        if (CATALOG.size() != 30 ||
            winter.atZone(ZoneId.of("Asia/Makassar")).getDayOfMonth() != 16 ||
            !winter.atZone(ZoneId.of("America/New_York")).getOffset().equals(ZoneOffset.ofHours(-5)) ||
            !summer.atZone(ZoneId.of("America/New_York")).getOffset().equals(ZoneOffset.ofHours(-4)) ||
            !summer.atZone(ZoneId.of("Europe/London")).getOffset().equals(ZoneOffset.ofHours(1))) {
            throw new IllegalStateException("Time zone check failed");
        }
        WorldClock widget = new WorldClock(true);
        try {
            widget.frame.validate();
            widget.frame.addNotify();
            widget.frame.validate();
            if (widget.cards.size() != 5 || !widget.frame.isAlwaysOnTop() ||
                widget.cards.get(0).dial.getWidth() <= 0) {
                throw new IllegalStateException("Initial widget layout check failed");
            }

            widget.moveCity(0, 1);
            if (!widget.cards.get(1).city.name().equals("Denpasar")) {
                throw new IllegalStateException("Reorder check failed");
            }
            widget.moveCity(1, 0);
            widget.frame.validate();
            for (CityCard card : widget.cards) {
                card.refresh(winter.atZone(card.city.zone()), LocalDate.of(2026, 1, 16), TIME_FORMATS[1]);
            }
            if (!widget.cards.get(0).digital.getText().equals("03:30:00") ||
                !widget.cards.get(3).date.getText().endsWith("-1d")) {
                throw new IllegalStateException("Displayed time or date check failed");
            }
            widget.frame.validate();
            if (widget.cards.get(0).digital.getWidth() <= 0 || widget.cards.get(0).date.getWidth() <= 0) {
                throw new IllegalStateException("Clock label layout check failed");
            }
            BufferedImage image = new BufferedImage(widget.content.getWidth(), widget.content.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            try {
                widget.content.printAll(g);
            } finally {
                g.dispose();
            }
            javax.imageio.ImageIO.write(image, "png", output.toFile());
            widget.compact = true;
            widget.rebuildCards();
            widget.cities.clear();
            widget.rebuildCards();
            if (!widget.cards.isEmpty() || widget.list.getComponentCount() != 1) {
                throw new IllegalStateException("Empty state check failed");
            }
        } finally {
            widget.exit();
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length == 2 && args[0].equals("--verify")) {
            SwingUtilities.invokeAndWait(() -> {
                try {
                    verifyAndRender(Path.of(args[1]));
                } catch (IOException error) {
                    throw new java.io.UncheckedIOException(error);
                }
            });
            return;
        }
        SwingUtilities.invokeLater(() -> new WorldClock(false).showWidget());
    }
}

Use one instant for every city

A world clock displays the same moment in different places. The refresh method takes one snapshot of the system clock. Each card then converts that snapshot into a ZonedDateTime using its city’s IANA time-zone ID.

Instant now = Instant.now();
ZonedDateTime denpasar = now.atZone(ZoneId.of("Asia/Makassar"));
ZonedDateTime newYork = now.atZone(ZoneId.of("America/New_York"));

Using a region-based ZoneId lets Java apply the correct offset for that instant. New York, for example, can show either UTC−05:00 or UTC−04:00 depending on the date. The offset in each card comes from ZonedDateTime.getOffset().

To show whether a city is on a different calendar day, compare its LocalDate with the date in ZoneId.systemDefault(). A card shows -1d when its date is one day behind the computer’s local date.

Draw the clock face with Java 2D

The Dial component overrides paintComponent(). It moves the graphics origin to the center of the component and scales a 100-by-100 drawing area to fit. The same drawing code therefore works for both the regular and compact layouts.

The dial uses a gradient bezel, a shaded face, and sixty tick marks. Every fifth tick is longer and uses the city’s accent color. For each tick or hand, the following helper projects two points onto the same radial line.

private static Line2D radialLine(double angle, double start, double end) {
    double x = Math.sin(angle);
    double y = -Math.cos(angle);
    return new Line2D.Double(x * start, y * start, x * end, y * end);
}

At zero radians, sin(0) is zero and -cos(0) is negative, so the line points toward twelve o’clock. A quarter turn points right toward three o’clock. A negative starting radius extends the hand a little behind the center.

When seconds are enabled, the fractional part of the second comes from getNano(). The minute hand uses the fraction of the current minute, and the hour hand uses the fraction of the current hour.

double second = showSeconds ? time.getSecond() + time.getNano() / 1_000_000_000.0 : 0;
double minute = time.getMinute() + second / 60;
double hour = time.getHour() % 12 + minute / 60;

hand(g, hour / 12, 23, 4.3f, TEXT);
hand(g, minute / 60, 33, 2.9f, TEXT);
if (showSeconds) hand(g, second / 60, 36, 1.4f, accent);

The example uses a Swing Timer with a 50 ms interval to repaint the moving second hand. Each tick reads the current time. If the event dispatch thread is briefly busy, the next refresh uses the current instant and the clock catches up automatically.

Keep the widget visible

The widget uses an undecorated JFrame and calls setAlwaysOnTop(true) by default. A mouse listener on the title lets you drag the window. The Options menu lets you switch off always-on-top, hide seconds, choose a 12-hour format, or switch to a compact layout.

+ City opens the city picker. Each card’s menu can move the city up or down or remove it. Java’s Preferences API stores the city order, window position, and display settings. When the system tray is available, the hide button moves the widget there. Otherwise, the widget minimizes to the taskbar.

Run the example

To compile and run from source, use a JDK.

javac --release 17 -encoding UTF-8 WorldClock.java
java WorldClock

How do I write a simple analog clock using Java 2D?

Here is a simple Java code for an analog clock using Java 2D features in Swing.

Please adjust the code according to your needs. This code will create a new JFrame and continually update it every second with the current time.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class AnalogClock extends JPanel {

    public AnalogClock() {
        setPreferredSize(new Dimension(400, 300));
        setBackground(Color.WHITE);
        new Timer(1000, e -> repaint()).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int side = Math.min(getWidth(), getHeight());
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        GregorianCalendar time = new GregorianCalendar();
        int second = time.get(Calendar.SECOND);
        int minute = time.get(Calendar.MINUTE);
        int hour = time.get(Calendar.HOUR_OF_DAY);

        drawHand(g2d, side/2 - 10, second / 60.0, 0.5f, Color.RED);
        drawHand(g2d, side/2 - 20, minute / 60.0, 2.0f, Color.BLUE);
        drawHand(g2d, side/2 - 40, hour / 12.0, 4.0f, Color.BLACK);

        // Draw clock numbers and circle
        drawClockFace(g2d, centerX, centerY, side/2 - 40);
    }

    private void drawHand(Graphics2D g2d, int length, double value, float stroke, Color color) {
        double angle = Math.PI * 2 * (value - 0.25);
        int endX = (int) (getWidth() / 2 + length * Math.cos(angle));
        int endY = (int) (getHeight() / 2 + length * Math.sin(angle));

        g2d.setColor(color);
        g2d.setStroke(new BasicStroke(stroke));
        g2d.drawLine(getWidth() / 2, getHeight() / 2, endX, endY);
    }

    // Added method to draw the clock face and numbers
    private void drawClockFace(Graphics2D g2d, int centerX, int centerY, int radius) {
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.setColor(Color.BLACK);
        g2d.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);

        for (int i = 1; i <= 12; i++) {
            double angle = Math.PI * 2 * (i / 12.0 - 0.25);
            int dx = centerX + (int) ((radius + 20) * Math.cos(angle));
            int dy = centerY + (int) ((radius + 20) * Math.sin(angle));

            g2d.drawString(Integer.toString(i), dx, dy);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Analog Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new AnalogClock());
        frame.pack();
        frame.setVisible(true);
    }
}

In this code, each “hand” of the clock is moved by calculating its angle according to the current time. The drawHand() method takes the length of the hand, the proportion of its rotation (for an hour hand, this would be the current hour divided by 12), the stroke width to draw with, and the color to draw, then calculates the end point of the hand line and draws it from the center of the clock.

The drawClockFace(), which draws a circle using the drawOval() method (because a circle is a type of oval), and adds numbers around the edge of the circle using the drawString() method. In both cases, the position of each element is calculated based on the sine and cosine of the angle that represents each hour on the clock face.

Analog Clock
Analog Clock

Note: Swing is an old but reliable technology that allows coding of GUI elements, however, it’s no longer actively developed. If you are developing a new project, consider using JavaFX as it is more modern and actively developed.

Filtering JList Component Models

Filter items in a long list are often accomplished using the JTextField component. As the user inputs into the JTextField component, the set of items shown in the list is narrowed to just those things that correspond to the input received from the user.

It is necessary to utilize two elements to implement this function of the JList component, one of which is a model that filters a set of elements based on some text. The other executes the filter method when the user enters text.

Implementing the input field is a simpler job, so let’s start with it in our review of the implementation process. The JTextField component model is a document used with a Swing set of components. It is necessary to implement the DocumentListener interface in the model in order to monitor input to a Document. Text input, updating, and deletion are tracked using three methods defined below:

  • public void insertUpdate (DocumentEvent event)
  • public void changedUpdate (DocumentEvent event)
  • public void removeUpdate (DocumentEvent event)

When the model attributes are updated, the changedUpdate() method is used to update the model. It is possible that it will not be realized. In order to avoid duplicating filtering actions across all three methods, the generic method generated in the custom model is simply called by the other two. A detailed explanation of the JTextField component, which is used for filtering in the JList component, may be found in the following section:

JTextField input = new JTextField(); 
String lastSearch = ""; 

DocumentListener listener = new DocumentListener() { 
    public void insertUpdate(DocumentEvent event) { 
        Document doc = event.getDocument(); 
        lastSearch = doc.getText(0, doc.getLength()); 
        ((FilteringModel)getModel()).filter(lastSearch); 
    } 

    public void removeUpdate(DocumentEvent event) { 
        Document doc = event.getDocument(); 
        lastSearch = doc.getText(0, doc.getLength()); 
        ((FilteringModel)getModel()).filter(lastSearch); 
    }

    public void changedUpdate(DocumentEvent event) {
    } 
}; 

input.getDocument().addDocumentListener(listener);

In order to avoid being restricted to just using the JTextField component that was generated using the JList, the installJTextField() method is used, which attaches the event listener to the component that was built using the JList in the first place. In addition, a mechanism is provided to eliminate this match. Through the usage of these methods, the user of a filtering JList may choose to use their own JTextField in place of the default one.

public void installJTextField(JTextField input) { 
    input.getDocument().addDocumentListener(listener); 
} 

public void unnstallJTextField(JTextField input) { 
    input.getDocument().removeDocumentListener(listener); 
}

After that, the filtering model is taken into consideration. This case implements the filter() function, which is invoked by methods that implement the DocumentListener interface, as seen below. To put this strategy into action, you’ll need to have two lists of objects on hand: a source list and a filtered list of items. Because you are inheriting from the AbstractListModel class, you must implement some of the methods listed below in your code:

  • Constructor
  • Method for adding items to the model is being implemented in this project.
  • getElementAt() is used to get an element.
  • getSize() is used to retrieve sizes.
  • The constructor produces two instances of the List objects. The type of objects that are stored as List elements does not matter. Therefore List objects are generated to carry items of the following types:
List<Object> list; 
List<Object> filteredList; 

public FilteringModel() { 
    list = new ArrayList<>(); 
    filteredList = new ArrayList<>(); 
}

Model elements are added by adding them to the original model and then filtering the resulting model with the previously added elements. Optimization of this approach may be achieved by using a method to filter a single element when it is added; however, in this implementation, the filter() function is invoked when an element is added, which is also used to filter the whole list. (It should be noted that the event implementation in the DocumentListener also invokes the filter() method.) As a result, even when only one item is added to the list, the whole list is filtered, with each item that matches the search parameters being added to the filtered list.

public void addElement(Object element) { 
    list.add(element); 
    filter(); 
}

The size of the returned model is the same as the size of the filtered list, but not the same as the original:

public int getSize() { 
    return filteredList.size(); 
}

Similar to the technique for obtaining the size of a model, the method for obtaining an item from a list returns elements from the filtered list rather than the original list. In order to avoid having to go through the complete list, it has been implemented as follows:

public Object getElementAt(int index) { 
    Object returnValue; 
    if (index < filteredList.size()) { 
        returnValue = filteredList.get(index); 
    } else { 
        returnValue = null;
    } 
    return returnValue; 
}

Finally, the filter() method is responsible for most of the work. Because you have no way of knowing whether the new search string will broaden or limit the set of items, the quickest and most straightforward solution is to remove the whole filtered list and replace it with items that fit your search criteria from the original list. A match may be discovered at the beginning of a line as well as at any point throughout it. An example of searching for the letter “A” is shown below. This function enables you to locate items in a string that begin with the capital letter “A” or contain the letter “A” at any point in the string.

void filter(String search) {
    filteredList.clear();
    for (Object element: list) {
        if (element.toString().contains(search)) {
            filteredList.add(element); 
        } 
    } 
    fireContentsChanged(this, 0, getSize()); 
}

It is important to note that the search in this approach is case-sensitive. You may alter the method to implement a case-insensitive search and start the search at the beginning of the string.

After you have added entries to the filtered list, you may also sort the results. This operation requires your familiarity with the model’s contents. The function toString() is currently used by search, which does not indicate that it may include elements of a suitable type that can also be sorted when it is performed.

Here is a full implementation of the JList filter element with an inner class ListModel, as seen in the accompanying code sample. This class implements the DocumentListener interface, which the text component uses to listen for new documents. Although the addition of this class may seem needless at first look, given that filtering is only done for this model, the specification of behavior in this implementation is the most accurate.

package org.kodejava.swing;

import javax.swing.AbstractListModel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.util.ArrayList;
import java.util.List;

public class FilteringJList extends JList<Object> {
    private JTextField input;

    public FilteringJList() {
        setModel(new FilteringModel());
    }

    public void installJTextField(JTextField input) {
        if (input != null) {
            this.input = input;
            FilteringModel model = (FilteringModel) getModel();
            input.getDocument().addDocumentListener(model);
        }
    }

    public void uninstallJTextField(JTextField input) {
        if (input != null) {
            FilteringModel model = (FilteringModel) getModel();
            input.getDocument().removeDocumentListener(model);
            this.input = null;
        }
    }

    public void setModel(ListModel<Object> model) {
        if (!(model instanceof FilteringModel)) {
            throw new IllegalArgumentException();
        } else {
            super.setModel(model);
        }
    }

    public void addElement(Object element) {
        ((FilteringModel) getModel()).addElement(element);
    }

    private static class FilteringModel extends AbstractListModel<Object> implements DocumentListener {
        List<Object> list;
        List<Object> filteredList;
        String lastFilter = "";

        public FilteringModel() {
            list = new ArrayList<>();
            filteredList = new ArrayList<>();
        }

        public void addElement(Object element) {
            list.add(element);
            filter(lastFilter);
        }

        public int getSize() {
            return filteredList.size();
        }

        public Object getElementAt(int index) {
            Object returnValue;
            if (index < filteredList.size()) {
                returnValue = filteredList.get(index);
            } else {
                returnValue = null;
            }
            return returnValue;
        }

        void filter(String search) {
            filteredList.clear();
            for (Object element : list) {
                if (element.toString().contains(search)) {
                    filteredList.add(element);
                }
            }
            fireContentsChanged(this, 0, getSize());
        }

        public void insertUpdate(DocumentEvent event) {
            Document doc = event.getDocument();
            try {
                lastFilter = doc.getText(0, doc.getLength());
                filter(lastFilter);
            } catch (BadLocationException ble) {
                System.err.println("Bad location: " + ble);
            }
        }

        public void removeUpdate(DocumentEvent event) {
            Document doc = event.getDocument();
            try {
                lastFilter = doc.getText(0, doc.getLength());
                filter(lastFilter);
            } catch (BadLocationException ble) {
                System.err.println("Bad location: " + ble);
            }
        }

        public void changedUpdate(DocumentEvent event) {
        }
    }
}

It is now necessary to develop a test program. The following six lines will be crucial in the event. They build a JList component, attach it to the JScrollPane component, and then attach a text box to it as seen in the code:

FilteringJList list = new FilteringJList();
JScrollPane pane=new JScrollPane(list);
frame.add(pane,BorderLayout.CENTER);
JTextField text=new JTextField();list.installJTextField(text);
frame.add(text,BorderLayout.NORTH);

To the model, new components are introduced in the program’s primary body. The model shown below includes a list of Christmas gifts, the names of Santa’s reindeer, the names of London Underground lines, and the letters of the Greek alphabet.

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.EventQueue;

public class JListFiltersDemo {
    public static void main(String[] args) {
        Runnable runner = () -> {
            JFrame frame = new JFrame("Filtering List");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FilteringJList list = new FilteringJList();
            JScrollPane pane = new JScrollPane(list);
            frame.add(pane, BorderLayout.CENTER);
            JTextField text = new JTextField();
            list.installJTextField(text);
            frame.add(text, BorderLayout.NORTH);
            String[] elements = {
                    "Partridge in a pear tree", "Turtle Doves", "French Hens",
                    "Calling Birds", "Golden Rings", "Geese-a-laying",
                    "Swans-a-swimming", "Maids-a-milking", "Ladies dancing",
                    "Lords-a-leaping", "Pipers piping", "Drummers drumming",
                    "Dasher", "Dancer", "Prancer", "Vixen", "Comet", "Cupid",
                    "Donner", "Blitzen", "Rudolf", "Bakerloo", "Center",
                    "Circle", "District", "East London", "Hammersmith and City",
                    "Jubilee", "Metropolitan", "Northern", "Piccadilly Royal",
                    "Victoria", "Waterloo and City", "Alpha", "Beta", "Gamma",
                    "Delta", "Epsilon", "Zeta", "Eta", "Theta", "Iota", "Kappa",
                    "Lambda", "Mu", "Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma",
                    "Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega"};
            for (String element : elements) {
                list.addElement(element);
            }
            frame.setSize(500, 500);
            frame.setVisible(true);
        };
        EventQueue.invokeLater(runner);
    }
}
Filtering JList Component Models Demo
Filtering JList Component Models Demo

Because this filtering strategy is based on the JList component and its accompanying JTextField component, it will operate successfully if your list’s entries are appropriately displayed when you use the function toString(). Creating a Filter interface that is provided to the model when filtering operations are performed might be useful for doing more complicated filtering tasks.

In this example, the only item that is not addressed is the process of selection. By default, when the contents of the model list change, the JList does not update the selection of the model list. Filtering may be used to either retain the chosen item or emphasize the first item in the list, depending on the desired behavior.

Even though the original JList component does not explicitly offer the functionality, there are techniques to implement filtering. Overriding the getNextMatch() function allows you to alter the default behavior if you so want.