|
|
| swings |
Expert:nijin
i want a text area field in which copy,cut and paste action should be disabled without using the jpassword. ie user cannot cut/copy or paste in this field area. |
| Answers |
H friend,
import java.awt.*; import java.util.*; import java.awt.BorderLayout; import java.awt.Container; import java.util.Hashtable; import java.awt.TextArea; import java.awt.TextComponent; import javax.swing.*; import javax.swing.JScrollPane; import javax.swing.text.DefaultEditorKit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
public class CutPastAWT { public static void main(String args[]) { JFrame frame = new JFrame("simple Cut and paste Example");
Container content = frame.getContentPane(); JTextField textField = new JTextField("", 30); JTextArea textArea = new JTextArea(" ",4, 25); JScrollPane scrollPane = new JScrollPane(textArea);
content.add(textField, BorderLayout.NORTH); content.add(scrollPane, BorderLayout.CENTER);
Action actions[] = textField.getActions();
Action cutAction = TextAction.findAction(actions, DefaultEditorKit.cutAction); Action copyAction = TextAction.findAction(actions, DefaultEditorKit.copyAction); Action pasteAction = TextAction.findAction(actions, DefaultEditorKit.pasteAction);
JPanel panel = new JPanel(); content.add(panel, BorderLayout.SOUTH);
JButton cutButton = new JButton(cutAction); cutButton.setText("Cut"); panel.add(cutButton); cutButton.setMnemonic(KeyEvent.VK_D); cutButton.setActionCommand("disable"); JButton copyButton = new JButton(copyAction); copyButton.setText("Copy"); panel.add(copyButton);
copyButton.setMnemonic(KeyEvent.VK_E); copyButton.setActionCommand("enable"); copyButton.setEnabled(false); JButton pasteButton = new JButton(pasteAction); pasteButton.setText("Paste"); panel.add(pasteButton);
frame.setVisible(true); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
class TextAction { private TextAction(){ }
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); } } ------------------------------
Thanks,
Amardeep
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|