// ui/Popup.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Creating popup menus with Swing. import javax.swing.*; import java.awt.*; import java.awt.event.*; import static onjava.SwingConsole.*; public class Popup extends JFrame { private JPopupMenu popup = new JPopupMenu(); private JTextField t = new JTextField(10); public Popup() { setLayout(new FlowLayout()); add(t); ActionListener al = e -> t.setText(((JMenuItem)e.getSource()).getText()); JMenuItem m = new JMenuItem("Hither"); m.addActionListener(al); popup.add(m); m = new JMenuItem("Yon"); m.addActionListener(al); popup.add(m); m = new JMenuItem("Afar"); m.addActionListener(al); popup.add(m); popup.addSeparator(); m = new JMenuItem("Stay Here"); m.addActionListener(al); popup.add(m); PopupListener pl = new PopupListener(); addMouseListener(pl); t.addMouseListener(pl); } class PopupListener extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { maybeShowPopup(e); } @Override public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if(e.isPopupTrigger()) popup.show(e.getComponent(), e.getX(), e.getY()); } } public static void main(String[] args) { run(new Popup(), 300, 200); } }