CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > Adapter Classe in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

Adapter Classe in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: March 9, 2021 · 2 min read · 0 Comments
Share: in X

In the Java Swing library, adapter classes are a set of abstract classes that provide default implementations of listener interfaces. These adapter classes are best suited to use only the required methods to use by the developers. It does not force developers to override all the methods available in listener interfaces.

Adapter classes simplify the task of writing listener classes by providing default implementations for all the methods in an interface. This means that we can create a subclass of an adapter class and override only the methods that we are interested in implementing. This can save a lot of code, especially when we only need to handle a few of the events that a listener interface generates.

For example, the MouseAdapter class provides default implementations for all the methods of the MouseListener interface. If we want to handle only the mouse click event, we can extend the MouseAdapter class and override only the mouseClicked() method.

Following is an example of using a MouseAdapter class that handles the mouseClicked() event:

import java.awt.event.*;

public class MyMouseListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
        // Handle the mouse click event here
    }
}

// Create a component and add the listener
JComponent component = new JComponent();
component.addMouseListener(new MyMouseListener());

In this example, the MyMouseListener class extends the MouseAdapter class and overrides the mouseClicked() method to handle the mouse click event. Then, the listener is added to a JComponent using the addMouseListener() method.

There are many adapter classes. Some of them are given below:

  1. WindowAdapter – Provides default implementations of the WindowListener interface methods for handling window events.
  2. KeyAdapter – Provides default implementations of the KeyListener interface methods for handling keyboard events.
  3. FocusAdapter – Provides default implementations of the FocusListener interface methods for handling focus events.
  4. ComponentAdapter – Provides default implementations of the ComponentListener interface methods for handling component events.
  5. ContainerAdapter – Provides default implementations of the ContainerListener interface methods for handling container events.
  6. MouseMotionAdapter – Provides default implementations of the MouseMotionListener interface methods for handling mouse motion events.
  7. MouseWheelAdapter – Provides default implementations of the MouseWheelListener interface methods for handling mouse wheel events.
  8. ItemAdapter – Provides default implementations of the ItemListener interface methods for handling item events.
  9. MenuAdapter – Provides default implementations of the MenuListener interface methods for handling menu events.
  10. PopupMenuAdapter – Provides default implementations of the PopupMenuListener interface methods for handling popup menu events.

These adapter classes can be used in the same way as the MouseAdapter example I provided earlier. They all provide default implementations of the interface methods, making it easier to create listener classes for specific events.

Related Posts:

  • Abstract Class in Java: Bridging Code Reusability…
  • Interface in Java: Mastering Abstraction and…
  • Abstraction in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Event Handling in Java Swing
  • Inheritance in Java: A Developer’s Guide to Code Reusability
Tags:javaswing
Was this article helpful?
← Previous ArticleClean up space in the tableau server
Next Article →GridLayout in Java Swing

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap