OnJava8-Examples/gui/Popup.java
2015-05-06 12:09:38 -07:00

51 lines
1.3 KiB
Java

//: gui/Popup.java
// Creating popup menus with Swing.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static net.mindview.util.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);
}
} ///:~