Java examples for Swing:JPopupMenu
Add the specified text component with pop-up menus including select all, cut, copy and paste.
/*/* w ww . j a v a 2 s.com*/ * Copyright (c) 2011, 2020, Frank Jiang and/or its affiliates. All rights * reserved. SwingUtils.java is built in 2012-11-2. */ import java.awt.Component; import java.awt.Dialog; import java.awt.Dimension; import java.awt.FileDialog; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.lang.reflect.Method; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JToggleButton; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.filechooser.FileFilter; import javax.swing.text.JTextComponent; public class Main{ /** * Add the specified text component with pop-up menus including select all, * cut, copy and paste. (all in Chinese, zh_CH with no shortcuts) * * @param text * the specified text component */ public static void addTextJPopMenus(final JTextComponent text) { JPopupMenu popupMenu = new JPopupMenu(); addPopup(text, popupMenu); JMenuItem mntmSelectAll = new JMenuItem( Messages.getString("SwingUtils.1")); //$NON-NLS-1$ mntmSelectAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.selectAll(); } }); mntmSelectAll.setMnemonic('A'); popupMenu.add(mntmSelectAll); JMenuItem mntmCut = new JMenuItem( Messages.getString("SwingUtils.2")); //$NON-NLS-1$ mntmCut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.cut(); } }); mntmCut.setMnemonic('T'); popupMenu.add(mntmCut); JMenuItem mntmCopy = new JMenuItem( Messages.getString("SwingUtils.3")); //$NON-NLS-1$ mntmCopy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.copy(); } }); mntmCopy.setMnemonic('C'); popupMenu.add(mntmCopy); JMenuItem mntmPaste = new JMenuItem( Messages.getString("SwingUtils.4")); //$NON-NLS-1$ mntmPaste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.paste(); } }); mntmPaste.setMnemonic('P'); popupMenu.add(mntmPaste); } /** * Add the specified pop-up menu the specified component. * * @param component * the specified component * @param popup * the specified pop-up menu */ public static void addPopup(Component component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) showMenu(e); } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) showMenu(e); } private void showMenu(MouseEvent e) { popup.show(e.getComponent(), e.getX(), e.getY()); } }); } }