Hi

The following is a simplified example of the problem I'm working on. The frame 
contains a button that invokes a sub-dialog. The sub-dialog contains an item that, 
when pressed, displays a popup containing a JButton. The problem I have is that 
keyboard focus is not passed to the JButton so that e.g. pressing Enter or Space does 
not select the JButton. Also in the real example, the popup menu item is a JPanel 
rather than a JButton. The JPanel contains several sub-controls which should be 
navigable by keyboard (Tab, Shift-Tab, ...) -- a feature which works when the JPanel 
is not in a popup.

What I'd like to know is how to enable keyboard support on the panel within the popup. 
Any help or references gratefully received.

Julian

// ---------------------

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

public class PanelPopup extends JFrame {

    static class ButtonMenuItem extends JPanel implements MenuElement {
        public ButtonMenuItem() {
            super(new BorderLayout());
            add(new JButton("Press Me"), BorderLayout.CENTER);
        }

        public void processMouseEvent(MouseEvent event,MenuElement 
path[],MenuSelectionManager manager) {}
        public void processKeyEvent(KeyEvent event,MenuElement 
path[],MenuSelectionManager manager) {}
        public void menuSelectionChanged(boolean isIncluded) {}
        public MenuElement[] getSubElements() { return new MenuElement[0]; }
        public Component getComponent() { return this; }
    }

    static class TestDialog extends JDialog {
        public TestDialog(Frame parent) {
            super(parent, "", true);
            final JPopupMenu menu = new JPopupMenu();
            menu.add(new ButtonMenuItem());

            final JButton popupBtn = new JButton("Menu >");
            popupBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    menu.show(popupBtn, 0, popupBtn.getHeight());
                }
            });

            final JButton closeBtn = new JButton("Close");
            closeBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    TestDialog.this.setVisible(false);
                }
            });

            JPanel panel = new JPanel(new BorderLayout());
            panel.add(popupBtn, BorderLayout.NORTH);
            panel.add(closeBtn, BorderLayout.SOUTH);
            this.getContentPane().add(panel);
            this.setSize(150, 100);
        }
    }


    public PanelPopup() {
        JPanel panel = new JPanel(new FlowLayout());
        final TestDialog dlg = new TestDialog(this);
        final JButton btn = new JButton("Sub-dialog...");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dlg.setVisible(true);
            }
        });
        panel.add(btn);
        getContentPane().add(panel);
        pack();
    }

    public static void main(String[] args) {
        PanelPopup tester = new PanelPopup();
        tester.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { System.exit(0); }
        });

        tester.setVisible(true);
    }
}
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to