Insubstantial / insubstantial

Swing look-and-feel library and assorted widgets
193 stars 58 forks source link

JTree Keyboard Traversal On Mac OS X #113

Open mochaman opened 11 years ago

mochaman commented 11 years ago

When a child node is selected in a JTree, the left arrow key should select the parent node, another press of the left arrow should collapse the parent, etc. This doesn't work in Insubstantial and makes navigating a tree with the keyboard very difficult. I only see this problem on Mac OS X (both Oracle and Apple JVMs).

I worked around it in my project by adding the following code to my JTree subclass:

InputMap im = this.getInputMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "selectParent");

It's possible other navigation keys are failing however this is the one I need/notice.

Quick JTree Example:

import java.awt.BorderLayout; import java.awt.EventQueue; import java.util.Hashtable; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.UIManager; import org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel;

public class TreeTest { public static void main(String[] args) {

    Runnable r = new Runnable() {

        public void run() {
            try {
                UIManager.setLookAndFeel(new SubstanceGraphiteGlassLookAndFeel());
            } catch (Exception e) {
                e.printStackTrace();
            }

            JFrame jf = new JFrame("JTree");
            jf.getContentPane().setLayout(new BorderLayout());
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Hashtable data = new Hashtable();
            String[] nodes = {"Child 1", "Child 2", "Child 3"};
            data.put("Parent Node", nodes);

            JTree tree = new JTree(data);

            jf.getContentPane().add(tree, BorderLayout.CENTER);

            jf.setSize(400,350);
            jf.setVisible(true);
        }
    };
    EventQueue.invokeLater(r);
}

}