Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.swing.*;

import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Main {
    public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) {
        final JPopupMenu popupMenu = new JPopupMenu();

        /* text fields popup menu: "Cut" */
        final JMenuItem cutMenuItem = new JMenuItem("Cut", 't');
        cutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final Component c = popupMenu.getInvoker();

                if (c instanceof JTextComponent) {
                    ((JTextComponent) c).cut();
                }
            }
        });
        popupMenu.add(cutMenuItem);

        /* text fields popup menu: "Copy" */
        final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
        copyMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final Component c = popupMenu.getInvoker();

                if (c instanceof JTextComponent) {
                    ((JTextComponent) c).copy();
                }
            }
        });
        popupMenu.add(copyMenuItem);

        /* text fields popup menu: "Paste" */
        final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
        pasteMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final Component c = popupMenu.getInvoker();

                if (c instanceof JTextComponent) {
                    ((JTextComponent) c).paste();
                }
            }
        });
        popupMenu.add(pasteMenuItem);
        popupMenu.addSeparator();

        /* text fields popup menu: "Select All" */
        final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A');
        selectAllMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final Component c = popupMenu.getInvoker();

                if (c instanceof JTextComponent) {
                    ((JTextComponent) c).selectAll();
                }
            }
        });
        popupMenu.add(selectAllMenuItem);

        /* add mouse listeners to the specified fields */
        for (final JTextComponent f : fields) {
            f.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    processMouseEvent(e);
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    processMouseEvent(e);
                }

                private void processMouseEvent(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                        popupMenu.setInvoker(f);
                    }
                }
            });
        }
        return popupMenu;
    }
}