Swing Key Adapter



Swing Key Adapter

  1. KeyAdapter class is an abstract adapter class in Java Swing.
  2. KeyAdapter class is for receiving keyboard events.
  3. Methods specified in KeyAdapter class are empty.
  4. KeyAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.KeyAdapter class –

public abstract class KeyAdapter
   extends Object
      implements KeyListener

Class constructors

KeyAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1keyPressed()voidKeyEventkey has been pressed
2keyReleased()voidKeyEventkey has been released
3keyTyped()voidKeyEventkey has been typed

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Key Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("KeyAdapter Example");

      final JTextField textField = new JTextField(10);

      JButton displayButton = new JButton("Display");
      
      displayButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            msgLabel.setText("You entered : " + textField.getText());
         }
      });
      
      textField.addKeyListener(new KeyAdapter() {
         public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
               msgLabel.setText("You entered : " + textField.getText());
            }
         }
      });
      
      controlPanel.add(textField);
      controlPanel.add(displayButton);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

key_adapter