01: package org.dbbrowser;
02:
03: import javax.swing.*;
04: import javax.swing.text.*;
05: import javax.swing.plaf.*;
06: import java.awt.*;
07: import java.awt.event.*;
08:
09: public class Popup {
10: public static void main(String args[]) {
11: Runnable runner = new Runnable() {
12: public void run() {
13: System.out.println("Starting...");
14: JFrame frame = new JFrame("Popup Example");
15: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16:
17: final JPopupMenu popup = new JPopupMenu();
18: JMenuItem menuItem1 = new JMenuItem("Option 1");
19: popup.add(menuItem1);
20:
21: JMenuItem menuItem2 = new JMenuItem("Option 2");
22: popup.add(menuItem2);
23:
24: final JTextField textField = new JTextField();
25: frame.getContentPane().add(textField,
26: BorderLayout.NORTH);
27:
28: ActionListener actionListener = new ActionListener() {
29: public void actionPerformed(ActionEvent actionEvent) {
30: try {
31: int dotPosition = textField
32: .getCaretPosition();
33: Rectangle popupLocation = textField
34: .modelToView(dotPosition);
35: popup.show(textField, popupLocation.x,
36: popupLocation.y);
37: } catch (BadLocationException badLocationException) {
38: System.err.println("Oops");
39: }
40: }
41: };
42: KeyStroke keystroke = KeyStroke.getKeyStroke(
43: KeyEvent.VK_PERIOD, 0, false);
44: textField.registerKeyboardAction(actionListener,
45: keystroke, JComponent.WHEN_FOCUSED);
46:
47: frame.setSize(250, 150);
48: frame.setVisible(true);
49: }
50: };
51: EventQueue.invokeLater(runner);
52: }
53: }
|