List of usage examples for javax.swing.text JTextComponent addMouseListener
public synchronized void addMouseListener(MouseListener l)
From source file:Main.java
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(); }/* w w w . ja v a 2 s.c om*/ } }); 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; }
From source file:gg.pistol.sweeper.gui.component.DecoratedPanel.java
/** * Helper method to add a contextual menu on a text component that will allow for Copy & Select All text actions. */// ww w .j a v a 2s .com protected void addCopyMenu(final JTextComponent component) { Preconditions.checkNotNull(component); if (!component.isEditable()) { component.setCursor(new Cursor(Cursor.TEXT_CURSOR)); } final JPopupMenu contextMenu = new JPopupMenu(); JMenuItem copy = new JMenuItem(component.getActionMap().get(DefaultEditorKit.copyAction)); copy.setText(i18n.getString(I18n.TEXT_COPY_ID)); contextMenu.add(copy); contextMenu.addSeparator(); JMenuItem selectAll = new JMenuItem(component.getActionMap().get(DefaultEditorKit.selectAllAction)); selectAll.setText(i18n.getString(I18n.TEXT_SELECT_ALL_ID)); contextMenu.add(selectAll); component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { contextMenu.show(component, e.getX(), e.getY()); } } }); }