Solved

Keyboard up/down arrow does not work for popup menus

Posted on 2002-04-17
3
177 Views
Last Modified: 2010-03-31
I am having a JTree. On the right click of each node i am displaying a popup menu.
PopUp menu is getting displayed when i right click, but than if I press the up or down
arrow, the options in popup menu is not getting selected instead the treenode selection
is getting changed while popup menu still remains on the scree.

I want that on pressing up or down key, popup menu options should get selected if popup
menu is visible, else if popupmenu is not visible than the respective treenode should get
selected.

Any help will be welcome.

Thanks in advance
0
Comment
Question by:ugarg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6949686
Here is working fine. Post your code or a sample code like this to give me a hint.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopupKey extends JFrame {
  private JTree tree;
  private JPopupMenu popup;
 
     public PopupKey() {
          init();
     }
     
     private void init() {
          getContentPane().setLayout(new BorderLayout());
          setSize(400, 300);
          setLocation(300, 100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    tree = new JTree();
    popup = new JPopupMenu("Demo");
    popup.add(new JMenuItem("Action 1"));
    popup.add(new JMenuItem("Action 2"));
    popup.add(new JMenuItem("Action 3"));
    popup.add(new JMenuItem("Action 4"));
    popup.add(new JMenuItem("Action 5"));
    popup.add(new JMenuItem("Action 6"));
    popup.add(new JMenuItem("Action 7"));
    getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
    tree.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) { checkPopup(me); }
      public void mouseReleased(MouseEvent me) { checkPopup(me); }
      protected void checkPopup(MouseEvent me) {
        if(me.isPopupTrigger()) {
         popup.show(tree, me.getX(), me.getY());
        }
      }
    });
    setVisible(true);
  }
 
     public static void main(String[] args) {
          PopupKey t = new PopupKey();    
     }
     
}
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 75 total points
ID: 6949817
It sounds like the Tree had still maintained focus.  My guess is that you had activated the pop-up, it is not modular, and no focus-grabbing routines were activated.  Try grabbing or setting the focus when you activate/show the pop-up.  Then make sure that you return the focus to the tree when leaving the pop-up.

Tom
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6953581
I guess that my comment helped you.

Thanks for the grade!
Tom
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question