JTree added to a JPopupMenu looses key arrow up-down navigation

Hi,

I add a JTree to a JPopupMenu. After a button is clicked it shows/hides. The problem is that the arrow up/down key navigation for JTree does not work if it is added to the JPopupMenu. Does anyone know a solution for this?

Here is a simple code example:

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

import javax.swing.JScrollPane;

import javax.swing.JTree;

publicclass PopupExampleextends javax.swing.JFrame{

protectedboolean isPopupVisible =false;

protected JPopupMenu popup;

protected JTree tree;

protected JScrollPane scroll;

public PopupExample(){

initComponents();

popup =new JPopupMenu();

tree =new JTree();

tree.setMinimumSize(new Dimension(290, 190));

tree.setPreferredSize(new Dimension(290, 190));

scroll =new JScrollPane(tree);

popup.setFocusable(false);

popup.setOpaque(false);

popup.setInvoker(jButton1);

popup.add(scroll);

jButton1.setAction(new AbstractAction(){

publicvoid actionPerformed(ActionEvent event){

popup.setPopupSize(300, 200);

popup.setPreferredSize(new Dimension(300, 200));

if (!isPopupVisible){

popup.setVisible(true);

tree.requestFocusInWindow();

isPopupVisible =true;

}else{

popup.setVisible(false);

isPopupVisible =false;

}

}

});

}

privatevoid initComponents(){

jButton1 =new javax.swing.JButton();

jScrollPane1 =new javax.swing.JScrollPane();

jTree1 =new javax.swing.JTree();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");

jScrollPane1.setViewportView(jTree1);

org.jdesktop.layout.GroupLayout layout =new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap(194, Short.MAX_VALUE)

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.add(jButton1)

.add(48, 48, 48))

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 183, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.add(23, 23, 23))))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.add(57, 57, 57)

.add(jButton1)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 13, Short.MAX_VALUE)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

pack();

}

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new PopupExample().setVisible(true);

}

});

}

private javax.swing.JButton jButton1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTree jTree1;

Thx for help.

Squibber

[5696 byte] By [squibbera] at [2007-11-27 6:27:27]
# 1
> popup.setFocusable(false);That's why.
quittea at 2007-7-12 17:49:19 > top of Java-index,Java Essentials,Java Programming...
# 2

Hm, if I remove popup.setFocusable(false);

and I click anywhere within the JTree the popup hides now immediately. Maybe this is the default behaviour of a JPopupMenu but it is not what I need here.

The user should be able to select one or more nodes from the JTree and if the popup should be hided the user can click on the button again.

squibbera at 2007-7-12 17:49:19 > top of Java-index,Java Essentials,Java Programming...
# 3
> I click anywhere within the JTree the popup hides now immediatelyDid you change anything else? Using your example, I can click into the popup without closing it, and it hides when I click into the main window (Java 1.4, 1.5).
quittea at 2007-7-12 17:49:19 > top of Java-index,Java Essentials,Java Programming...
# 4
You are right. In my example it works. However it did not work in my application. But this was another matter. Now everything is fine and finally the focusable setting was the clue to it. thx.squibber
squibbera at 2007-7-12 17:49:19 > top of Java-index,Java Essentials,Java Programming...