Friday, April 17, 2009

Implementing Action Class in Swing Components






The article enlightens us all with how you can implement Action Class in the Swing Components.

If you are aware of the swing components, you must have known that swing components do not allow us to edit the text underlying with in it without keyboard.

If you see the figure above, after right click on the textarea components or the textfield component, the option to copy, cut, paste, select all and delete all options are appearing.
This functionality does not come with default swing components.

You need to apply the Action class and DefaultEditorKit class to get it done.

DefaultEditorKit class is the class which is having a set of things needed by a text component to be functioning for some type of text component.

Without any further description of how we achieve this, I would like to bring the code for this:

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JPopupMenu;
import javax.swing.Action;
import java.util.Hashtable;
import javax.swing.border.LineBorder;
import javax.swing.text.DefaultEditorKit;

public class ActionDemo extends JFrame implements MouseListener {
JPopupMenu jPopupMenu = new JPopupMenu();
ActionDemo(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}

JTextField jTextField1 = new JTextField(20);
JTextField jTextField2 = new JTextField(20);
JTextArea jTextArea = new JTextArea(5,20);
JScrollPane jScrollPane = new JScrollPane();

jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
jScrollPane.getViewport().add(jTextArea,null);
this.setTitle("ActionDemo");
this.setSize(300,75);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.add(jTextField1,BorderLayout.NORTH);
this.add(jTextField2,BorderLayout.CENTER);
this.add(jScrollPane,BorderLayout.SOUTH);
this.pack();
Action actions[] = jTextField1.getActions();
Action cutActiontf1 = TextUtilities.findAction(actions,
DefaultEditorKit.cutAction);
Action copyActiontf1 = TextUtilities.findAction(actions,
DefaultEditorKit.copyAction);
Action pasteActiontf1 = TextUtilities.findAction(actions,
DefaultEditorKit.pasteAction);
Action selectAllActiontf1 = TextUtilities.findAction(actions,
DefaultEditorKit.selectAllAction);
Action deleteActiontf1 = TextUtilities.findAction(actions,
DefaultEditorKit.deleteNextCharAction);

jPopupMenu.add(copyActiontf1);
jPopupMenu.add(cutActiontf1);
jPopupMenu.add(pasteActiontf1);
jPopupMenu.add(selectAllActiontf1);
jPopupMenu.add(deleteActiontf1);

jTextField1.addMouseListener(this);
jTextField2.addMouseListener(this);
jTextArea.addMouseListener(this);

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

public void mouseClicked(MouseEvent me){

}

public void mouseReleased(MouseEvent me){
if(me.getButton() == MouseEvent.BUTTON3){
jPopupMenu.show(me.getComponent(),me.getX(),me.getY());
}
}

public void mousePressed(MouseEvent me){

}

public void mouseEntered(MouseEvent me){

}

public void mouseExited(MouseEvent me){

}

public static void main(String args[]){
ActionDemo actionDemo = new ActionDemo();
}
}

class TextUtilities {
private TextUtilities() {
}

public static Action findAction(Action actions[], String key) {
Hashtable commands = new Hashtable();
for (int i = 0; i < actions.length; i++) {
Action action = actions[i];
commands.put(action.getValue(Action.NAME),action);
}
return (Action) commands.get(key);
}
}


Hope you all will find it useful.

Total Pageviews