Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Adding an Input Map to a Component

                         

In this section, you will learn about the input map. Input map is used to perform actions according to the key strokes that mean when you press any combination of keys the program performs a specific task for the specified component. Specific task for the component is assigned for the specific key combination.

This program shows how to set the key strokes for the specific component to perform a specific task. This program represents a text area on the frame in which you can write text and perform some operation on the text by applying some specified short-cut keys. These keys perform some operations like: cut, copy, paste, up, down, left and right. Following methods and APIs have been used in this program to perform some specific tasks according to the specified key combination:

InputMap:
This is the class of javax.swing.* package. This class is used to create an input map. It performs the work with the ActionMap. And input map is created for performing some specific task according to the specified key combination.

getInputMap():
This is the method of the JComponent class which returns the input map when the component is focused.

getKeyStroke():
This is the method of KeyStroke class which is imported from the javax.swing.*; package. This method returns the instance of the KeyStroke class which determines the specified key combination to perform the specified operations. This method takes different-different arguments. Here, the method has taken two arguments, first is the numeric key code and another is the modifier.

put(KeyStroke key, Object action_name):
This is the method of the InputMap class. This method add key combination to an action map. It takes two arguments in which, first is the object of the KeyStroke class and another is the action name which has to be performed.

DefaultEditorKit.cutAction:
This is the class of javax.swing.* package which is used for applying cut operations on the text. This class cuts the text from the text area and stores text to the system clipboard after applying the operation.

DefaultEditorKit.copyAction:
This is the class which is used to applying copy operation for the specified text of the text area. This class simply send the selected text to the system clipboard.

DefaultEditorKit.pasteAction:
This is the class which is used to the paste the system clipboard text to the specified component.

Here is the code of program:

import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.*;

public class InputMapComponents{
  JFrame frame;
  JPanel panel;
  JLabel label;
  JTextArea area;
  JScrollPane pane;
  InputMap map;
  public static void main(String[] args) {
    InputMapComponents m = new InputMapComponents();
  }
  public InputMapComponents(){
    frame = new JFrame(" Adding an InputMap to a Component");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();  
    area = new JTextArea(7,10);
    pane = new JScrollPane(area);
    map = area.getInputMap();
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK);
        map.put(key, DefaultEditorKit.cutAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.copyAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.pasteAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_L, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.backwardAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.forwardAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.upAction);
    key = KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK);
    map.put(key, DefaultEditorKit.downAction);
    String lbl = "<html><b>" "Ctrl+p = paste" "<br>" "Ctrl+y = copy" 
"<br>" 
"Ctrl+c = cut" "<br>"
 
"Ctrl+l = cursor shift left one character" "<br>" "Ctrl+r = cursor shift 
right one character" 
"<br>"
 
"Ctrl+u = cursor shift up one line" "<br>" "Ctrl+d = cursor shift down 
one line" 
"</b></html>";
    JLabel label = new JLabel(lbl);
    panel.add(pane);
    panel.add(label);
    frame.add(panel);
    frame.setSize(400,200);
    frame.setVisible(true);
  }
}

Screen shot for the result of the above program:

Set the input map for the text area in Swing application

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.