保持专注,而不是在顶级Java

时间:2014-12-16 18:19:49

标签: java swing focus awt

我希望做一些可能不可能的事情;在Java(1.6在Windows 7上运行,因为这是依赖于平台的),我希望在另一个窗口上显示一个窗口,但不要从触发组件中窃取焦点。在下面附带的示例中,我希望能够单击文本字段,让新弹出窗口显示在前面,但保持焦点在文本字段上。我反而注意到的是,我将面板放在前面,但不再重点关注文本字段。

我主要想知道这是否可行(通常前窗有焦点在Windows,所以我倾向于可能没有)。如果没有,但有人对一个好的解决方法有意见,我会张开耳朵。

示例:

public class PopUpExample
{
    // Global toolkit listener.
    enum PopUp
    {
        INSTANCE;

        private PopUpWindow m_popUp;
        private JTextComponent m_textComponent;

        public void initialize(PopUpWindow p)
        {
            m_popUp = p;

            Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
            {
                public void eventDispatched(AWTEvent e)
                {
                    // Ensure event is a focus gain event.
                    if (( e instanceof FocusEvent )
                            && ((FocusEvent)e).getID()==FocusEvent.FOCUS_GAINED)
                    {
                        // If it is on a text field, make the pop up appear, but maintain focus on the text field
                        if ( (e.getSource() instanceof JTextComponent) )
                        {
                            m_textComponent = (JTextComponent)e.getSource();
                            // FIXME Code below here should set the button on top, yet leave the text field with focus.
                            m_popUp.setAlwaysOnTop( true );
                            m_popUp.setFocusable( false );
                            m_popUp.setVisible( true );
                            m_textComponent.requestFocus();
                            // end FIXME
                        }
                        // Otherwise, make the pop up disappear (if it isn't the pop up itself).
                        else if (((JComponent)e.getSource()).getRootPane().getComponent(0) instanceof PopUpWindow)
                        {
                            m_popUp.setVisible( false );
                        }
                    }
                }
            }, AWTEvent.FOCUS_EVENT_MASK);
        }
    }

    // Pop up window that isn't focusable
    class PopUpWindow extends JFrame
    {
        public PopUpWindow()
        {
            super();

            BorderLayout layout = new BorderLayout();
            this.setLayout( layout );

            this.setMinimumSize( new Dimension( 100, 100 ) );

            JButton button = new JButton("WantOnFront");
            button.setFocusable( false );
            this.add( button, BorderLayout.CENTER );

            this.setFocusable( false );
        }
    }

    // Main application window.
    class GuiWindow extends JFrame
    {
        public GuiWindow()
        {
            super();

            BorderLayout layout = new BorderLayout();
            this.setLayout( layout );

            this.setMinimumSize( new Dimension( 400, 400 ) );

            JButton button = new JButton("defaultFocusButton");
            this.add( button, BorderLayout.CENTER );

            JTextField textField = new JTextField("WantToMaintainFocusWhenClicked");
            this.add( textField, BorderLayout.SOUTH );
        }
    }

    // Setup code
    public PopUpExample()
    {
        new GuiWindow().setVisible( true );
        PopUp.INSTANCE.initialize( new PopUpWindow() );
    }

    public static void main( String[] args )
    {
        new PopUpExample();
    }
}

1 个答案:

答案 0 :(得分:4)

  

新的弹出窗口显示在前面,但保持对文本字段的重点。

JDialog dialog = new JDialog(...);
dialog.setFocusableWindowState( false );
...
dialog.setVisible( true );