Java Swing Menu enableMenu(JTextComponent text)

Here you can find the source of enableMenu(JTextComponent text)

Description

enable Menu

License

Open Source License

Declaration

public static void enableMenu(JTextComponent text) 

Method Source Code

//package com.java2s;
/**********************************************************************************************
 *
 * Asprise OCR Java API/*w ww  . j  a  va 2 s.co m*/
 * Copyright (C) 1998-2015. Asprise Inc. <asprise.com>
 *
 * This file is licensed under the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * You should have received a copy of the GNU Affero General Public License.  If not, please
 * visit <http://www.gnu.org/licenses/agpl-3.0.html>.
 *
 **********************************************************************************************/

import javax.swing.AbstractAction;
import javax.swing.Action;

import javax.swing.JPopupMenu;

import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

import java.awt.event.ActionEvent;

public class Main {
    public static void enableMenu(JTextComponent text) {
        final Action actionCopySelected = text.getActionMap().get(DefaultEditorKit.copyAction);
        final Action actionSelectAll = text.getActionMap().get(DefaultEditorKit.selectAllAction);
        final Action actionUnselect = text.getActionMap().get("unselect");
        Action actionCopyAll = new AbstractAction("Copy All to System Clipboard") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (actionSelectAll != null) {
                    actionSelectAll.actionPerformed(e);
                }
                if (actionCopySelected != null) {
                    actionCopySelected.actionPerformed(e);
                }
                if (actionUnselect != null) {
                    actionUnselect.actionPerformed(e);
                }
            }
        };

        JPopupMenu popupMenu = new JPopupMenu();
        if (actionCopySelected != null) {
            actionCopySelected.putValue(Action.NAME, "Copy Selected");
            popupMenu.add(actionCopySelected);
        }
        if (actionSelectAll != null && actionCopySelected != null) {
            popupMenu.add(actionCopyAll);
        }
        text.setComponentPopupMenu(popupMenu);
    }
}

Related

  1. createMenu(String label, int mnemonic, String description)
  2. createMenu(String label, Object... objects)
  3. createMenu(String name, Object[] item, int[] accelerator, ActionListener listener)
  4. createMenu(String text)
  5. createMenuKeyMask(final int aKeyStroke, final int... aMasks)
  6. findMenuBar(Container container)
  7. findMenuElement(final MenuElement root, final String text)
  8. fixIconTextGap(JComponent menu)
  9. getAllMenuSubElements( MenuElement root)