Java Reference
In-Depth Information
The lookup process requires getting the ActionMap for the component with getActionMap() ,
and then searching for the key with the get() method of ActionMap , as in this example:
Action readAction = component.getActionMap().get(DefaultEditorKit.readOnlyAction);
Listing 16-3. Using Text Component Actions
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class UseActions {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Use TextAction");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension empty = new Dimension(0,0);
final JTextArea leftArea = new JTextArea();
JScrollPane leftScrollPane = new JScrollPane(leftArea);
leftScrollPane.setPreferredSize(empty);
final JTextArea rightArea = new JTextArea();
JScrollPane rightScrollPane = new JScrollPane(rightArea);
rightScrollPane.setPreferredSize(empty);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftScrollPane, rightScrollPane);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu = new JMenu("Options");
menuBar.add(menu);
JMenuItem menuItem;
Action readAction =
leftArea.getActionMap().get(DefaultEditorKit.readOnlyAction);
menuItem = menu.add(readAction);
menuItem.setText("Make read-only");
Action writeAction =
leftArea.getActionMap().get(DefaultEditorKit.writableAction);
menuItem = menu.add(writeAction);
menuItem.setText("Make writable");
Search WWH ::




Custom Search