0

I would like to know if there is any way to detect a keyboard input while having a focus on JpopupMenu. This is to remove the focus on JPopupMenu whenever there is an input detection from keyboard. Is this possible?

Thank you.

Below is the simplified code I have written.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.net.*;

import java.io.*;


public class testClass {
   static JPopupMenu textPopupMenu = new JPopupMenu("MENU");
  final static JTextArea textInput = new JTextArea(50,80);

    final static JPanel overallPanel = new JPanel();
  final static JFrame overallFrame = new JFrame("Test");
    public static void main(String[] args)
    {
                SwingUtilities.invokeLater(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        final ActionListener actionListener1 = new ActionListener() {
                            public void actionPerformed(ActionEvent actionEvent) {
                                textPopupMenu.setFocusable(false);
                            }
                          };
                        KeyListener textInputListener = new KeyAdapter()
                        {
                            @Override
                            public void keyPressed(KeyEvent e)
                            {
                              //Get the suggested words from the function and populate them to the JMenuItem
                              textPopupMenu = new JPopupMenu("MENU");
                              for(int i=0;i<5;i++)
                              {
                                switch(i)
                                {
                                    case 0:
                                      JMenuItem item1 = new JMenuItem("A");
                                      textPopupMenu.add(item1);
                                      break;

                                    case 1:
                                      JMenuItem item2 = new JMenuItem("B");
                                      textPopupMenu.add(item2);
                                      break;

                                    case 2:
                                      JMenuItem item3 = new JMenuItem("C");
                                      textPopupMenu.add(item3);
                                      break;

                                    case 3:
                                      JMenuItem item4 = new JMenuItem("D");
                                      textPopupMenu.add(item4);
                                      break;

                                    case 4:
                                      JMenuItem item5 = new JMenuItem("E");
                                      textPopupMenu.add(item5);
                                      break;
                                };
                              }
                                textPopupMenu.setFocusable(true);
                                if (textPopupMenu.isVisible())
                                {
                                    textPopupMenu.setLocation(0, 0 + 20);
                                }
                                else
                                {
                                    textPopupMenu.show(textInput,0, 0 + 20);
                                }
                            }
                        };

                        textInput.addKeyListener(textInputListener);
                        overallPanel.add(textInput);

                        overallFrame.getContentPane().add(overallPanel);
                        overallFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);`enter code here`
                        overallFrame.setSize(1000, 900);
                        overallFrame.setLocationRelativeTo(null);
                        overallFrame.setVisible(true);
                    }
                });
    }

}
  • Add the same key listener to the `JPopupMenu`? – christopher May 08 '13 at 15:09
  • Hi Chris, I tried using this textPopupMenu.addKeyListener(textInputListener); but unfortunately, it won't listen to any input from keyboard while on focus. – Stephen Febrian May 08 '13 at 15:11
  • Some code might help us with this issue. – christopher May 08 '13 at 15:31
  • @ChrisCooney I have put my original simplified code above. Thanks. – Stephen Febrian May 08 '13 at 16:01
  • @StephenFebrian, using above code you will never detect input from Keyboard as you are implementing adapter and listening to every keyboard event. – Alpesh Gediya May 08 '13 at 18:32
  • @AlpeshGediya Do you mind to give some ideas on how to implement the textArea and popupmenu together? What I intend to do is whenever user keys-in the text, the pop-up menu will appear with selections (arrow keys are used to select the options from pop-up menu). However, when user keys in any other characters from keyboard, it must alternate the focus from popupmenu onto the textarea. Thanks once again. – Stephen Febrian May 09 '13 at 02:08

2 Answers2

1

I'm not quite sure why they did this, but you have to use JPopupMenu.addMenuKeyListener().

When I tested this, events were delivered twice, so I had to store the time of the last event obtained from event.getWhen() and only process events which were newer than the last stored time.

JWahle
  • 101
  • 4
0

Yes its possible to do it use registerKeyboardAction

  KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
  jpopMenu.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED);

JComponent.WHEN_FOCUSED will allow you to input detection from keyboard while JPopMenu is focused.

Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • Hi Alpesh, thank you for your answer. I tried your method, however it did not work. It's still focusing on JPopupMenu. I'm having a textarea in the background, and I'm not able to write anything in the textarea while JpopupMenu is still on focus. – Stephen Febrian May 08 '13 at 15:22
  • @StephenFebrian, make your jpopmenu modal to false. – Alpesh Gediya May 08 '13 at 15:28
  • @StephenFebrian Can you please paste your code so I can figure out what the issue is in you code? – Alpesh Gediya May 08 '13 at 15:31