JTextField: getActions() : JTextField « javax.swing « Java by API






JTextField: getActions()

 
import java.awt.BorderLayout;
import java.util.Hashtable;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;

public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();

    frame.add(textField, BorderLayout.NORTH);

    Action actions[] = textField.getActions();

    Action cutAction = findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = findAction(actions, DefaultEditorKit.pasteAction);

    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.SOUTH);

    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);

    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);

    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);

    frame.setSize(250, 250);
    frame.setVisible(true);
  }

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


           
         
  








Related examples in the same category

1.JTextField.LEFT
2.JTextField.LEADING
3.JTextField.TRAILING
4.new JTextField(String text)
5.new JTextField(String text, int columns)
6.JTextField: addActionListener(ActionListener s)
7.JTextField: addFocusListener(FocusListener l)
8.JTextField: addKeyListener(KeyListener l)
9.JTextField: copy()
10.JTextField: cut()
11.JTextField: getCaretPosition()
12.JTextField: getDocument()
13.JTextField: getHorizontalVisibility()
14.JTextField: getHorizontalVisibility() (2)
15.JTextField: getInputMap()
16.JTextField: getKeymap()
17.JTextField: getScrollOffset()
18.JTextField: paste()
19.JTextField: registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition)
20.JTextField: requestFocus()
21.JTextField: modelToView(int pos)
22.JTextField: read(Reader in, Object desc)
23.JTextField: setDocument(Document doc)
24.JTextField: setFocusAccelerator(char aKey)
25.JTextField: setHorizontalAlignment(int alignment)
26.JTextField: setInputVerifier(InputVerifier input)
27.JTextField: setPreferredSize(Dimension preferredSize)
28.JTextField: setNavigationFilter(NavigationFilter filter)
29.JTextField: setTransferHandler(TransferHandler newHandler)
30.JTextField: write(Writer out)