2

showing popup,

I want to show popup showing all the possible values if a particular token is not correct. This event is triggered when space bar is typed. It works fine. One bug is that, the popup pops up wherever the cursor is in the line, I want to show the popup only where the correct token should have been.

  String str = Australia Canberra Dollar  
  String tokens = str.split("\\s+");

  private void editorTextAreaKeyTyped(java.awt.event.KeyEvent evt) {
     if ((evt.getKeyChar() == KeyEvent.VK_SPACE)) {
        if(!tokens[0].equals("Australia")){
          showPopup(editor, menu);
      }
     }
   }


 public void showPopUpMenu(JTextArea jTextArea, List menuItems) {

    JPopupMenu popup = new JPopupMenu();
    Point point = getPoint(jTextArea);
    Caret caret = getCaret(jTextArea);
    JMenuItem menuItem;

    ActionListener al = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {                
            jTextArea.insert(e.getActionCommand(), jTextArea.getCaret().getDot());
        }
    };

    for (Object mi : menuItems) {
        menuItem = new JMenuItem(mi.toString());
        menuItem.addActionListener(al);
        popup.add(menuItem);
    }
    popup.show(jTextArea, point.x, point.y);
}
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 1
    I guess you can use `viewToModel()` and `modelToView()` for getting the point inside the JTextArea, where you want your **PopUp** to come up. Hopefully this [example](http://stackoverflow.com/a/10463120/1057230) can put more light on what I am saying. – nIcE cOw Sep 05 '12 at 15:09
  • The problem is different, The problem is with the string. If the strings are having particular positions, i can call the popup at that particular place. It is tokenized using [\\s+] so it is difficult to calculate the points where the token is started and where it ended. – FirmView Sep 05 '12 at 15:12
  • 1
    @Gagandeep Bali correctly suggests the `JTextComponent` methods that may be used to translate between document offset and view coordinates. I will defer to his answer. – trashgod Sep 05 '12 at 17:13
  • 1
    That code would not compile (even if it were not a snippet). For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 05 '12 at 17:30

0 Answers0