Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Combining JMenuItem and a KeyListener

  1. #1
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Question Combining JMenuItem and a KeyListener

    I have a JMenuItem "Find" in the Edit Menu.
    I want to add a shortcut key to the JMenuItem.

    For Find for example i want to use Ctrl + F

    Here is my 'Action' for 'Find' which can be used via Edit->Find. It works.

    Find = new AbstractAction(){
    		public void actionPerformed(ActionEvent e){
     
                        String word = JOptionPane.showInputDialog(new MainWindow(),"","Find",PLAIN_MESSAGE);
                         new WordSearcher(textArea,word);
                     }
    };

    WordSearcher() is a class i am using to search the word

    Now i want to add a KeyListener for ctrl+F
    for doing the same purpose.
    Even this one works.

     
    private KeyListener k1 = new KeyAdapter()
    	{       
    		public void keyPressed(KeyEvent e)
    		{   
                       if(e.isControlDown())
                            {
     
     
                                        if(e.getKeyCode()== KeyEvent.VK_F)
                                        {
                                            String word = JOptionPane.showInputDialog(new MainWindow(),"","Find",PLAIN_MESSAGE);
                                           new WordSearcher(textArea,word);
     
                                        }
     
     
                            }
                     }
    }

    But the problem here is i have to write the same code twice.
    Is there some way by which I can Use the already written Action Find in the KeyListener.
    Last edited by amitection; June 9th, 2014 at 01:20 AM.
    ăϻі†


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Combining JMenuItem and a KeyListener

    You can set the accelerator of the JMenuItem. An example of doing this can be found at
    How to Use Menus (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Combining JMenuItem and a KeyListener

    Quote Originally Posted by copeg View Post
    You can set the accelerator of the JMenuItem. An example of doing this can be found at
    How to Use Menus (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    I found the solution!!!

    Dont noe how it worked but it did

    private KeyListener k1 = new KeyAdapter()
    	{       
    		public void keyPressed(KeyEvent e)
    		{   
                       if(e.isControlDown())
                            {
     
     
                                        if(e.getKeyCode()== KeyEvent.VK_F)
                                        {
     
                                         Find.actionPerformed(null); 
                                        }
     
     
                            }
                     }
    }
    ăϻі†

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Combining JMenuItem and a KeyListener

    I found the solution!!!
    The code you posted does not indicate you even tried what I suggested in post #2 which would be the more appropriate way to create a menu shortcut. Using a KeyListener can result in a host of other issues (one of which would be Component focus). Why ask for advice and then seemingly ignore the advice you get?

  5. #5
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Combining JMenuItem and a KeyListener

    Quote Originally Posted by copeg View Post
    The code you posted does not indicate you even tried what I suggested in post #2 which would be the more appropriate way to create a menu shortcut. Using a KeyListener can result in a host of other issues (one of which would be Component focus). Why ask for advice and then seemingly ignore the advice you get?
    Sorry Sir, but no insults meant. I did read the link you gave me. But the issue with Find.setMnemonic(KeyEvent.VK_F); was that it was working only with ALT. I wanted the key to work with CNTRL. Thats it.
    ăϻі†

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Combining JMenuItem and a KeyListener

    Quote Originally Posted by amitection View Post
    But the issue with Find.setMnemonic(KeyEvent.VK_F); was that it was working only with ALT. I wanted the key to work with CNTRL. Thats it.
    Setting the mnemonic and setting the accelerator are two different things, and my post alluded to setting the accelerator:

    myMenuItem.setAccelerator(KeyStroke.getKeyStroke(K eyEvent.VK_F, InputEvent.CTRL_MASK));

    And the API for setAccelerator:

    Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy.

Similar Threads

  1. HELP with my JMenuItem opening a URL.
    By MR bruto in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 7th, 2013, 06:53 PM
  2. JMenuItem action Listener
    By WackoMako in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2012, 12:59 PM
  3. Getting an error when opening a new Window through a JMenuItem
    By !Marcus in forum Java Theory & Questions
    Replies: 3
    Last Post: December 31st, 2011, 07:51 PM
  4. Combining Shapes + Text
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: February 17th, 2011, 10:15 AM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM

Tags for this Thread