e614. Setting the Initial Focused Component in a Window

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了e614. Setting the Initial Focused Component in a Window相关的知识,希望对你有一定的参考价值。

There is no straightforward way to set the initial focused component in a window. The typical method is to add a window listener to listen for the window opened event and then make the desired component request the focus.

    // Create frame and three buttons
    JFrame frame = new JFrame();
    JButton component1 = new JButton("1");
    JButton component2 = new JButton("2");
    JButton component3 = new JButton("3");
    
    // Set component with initial focus; must be done before the frame is made visible
    InitialFocusSetter.setInitialFocus(frame, component2);
    
    class InitialFocusSetter {
        public static void setInitialFocus(Window w, Component c) {
            w.addWindowListener(new FocusSetter(c));
        }
    
        public static class FocusSetter extends WindowAdapter {
            Component initComp;
            FocusSetter(Component c) {
                initComp = c;
            }
            public void windowOpened(WindowEvent e) {
                initComp.requestFocus();
    
                // Since this listener is no longer needed, remove it
                e.getWindow().removeWindowListener(this);
            }
        }
    }

 

Related Examples

以上是关于e614. Setting the Initial Focused Component in a Window的主要内容,如果未能解决你的问题,请参考以下文章

e578. Setting the Clipping Area with a Shape

e611. Setting Focus Traversal Keys for the Entire Application

ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT settin

(translation.E004) You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUA

Codeforces Round #614 (Div. 2) E. Xenon's Attack on the Gangs

Codeforces Round #614 (Div. 2) A - ConneR and the A.R.C. Markland-N