Home Java Example Java Swing Capturing screen shot
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Capturing screen shot

Advertisement
In this section, you will learn how to capture the screen.

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Capturing screen shot   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
HEMANT UPADHYAY
February 22, 2012
java popup

java popup
Kalidas
March 28, 2012
Java(Swing);

1 No notes ahet............
Marlon Monzon
June 3, 2012
Wrong Topic

This page is about the Adding an Input Map to a Component and not about the screenshot. Please fix. Thank you very much RoseIndia.net.
DMCA.com