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

