简体   繁体   English

加载面板时专注于组件

[英]Focus on component when a panel is loaded

I have a frame, where i load a panel into. 我有一个框架,可以在其中装入面板。 It works fine, but nothing has focus when it loads. 它工作正常,但加载时没有任何焦点。 Pressing tab doesn't help. 按Tab键无济于事。 I have to use the mouse to press a textfield. 我必须使用鼠标来按下文本字段。 I've tried: jtextfield1.requestFocus(); 我试过了: jtextfield1.requestFocus(); and jtextfiel1.requestFocusInWindow(); jtextfiel1.requestFocusInWindow(); But it doesn't work. 但这是行不通的。

What am I doing wrong? 我究竟做错了什么?

The constructor in the JPanel : JPanel的构造函数:

public OpretOpdater(BrugerHandler brugerHandler, ReklamationHandler reklamationsHandler) {
    initComponents();
    jTextFieldOrdnr.requestFocusInWindow();
    this.brugerHandler = brugerHandler;
    this.rekH = reklamationsHandler;
    startUp();
}

Putting the panel in the frame in the GUI: 将面板放在GUI的框架中:

public static void opret(ReklamationHandler reklamationHandler) {
    rHandler = reklamationHandler;
    SwingUtilities.invokeLater(opret);
}

static Runnable opret = new Runnable() {
    @Override
    public void run() {
        JFrame f = jframe;
        f.getContentPane().removeAll();
        JPanel opret = new OpretOpdater(bHandler, rHandler);
        f.getContentPane().add(opret);
        f.pack();
        f.setLocationRelativeTo(null);
    }
};

You should call requestFocusInWindow() only when components are visible/shown on a container or after pack() has been called and all components are added to the container or else it wont work. 仅当在容器上可见/显示组件时,或者在调用pack()并将所有组件添加到容器中否则它不起作用时,才应调用requestFocusInWindow()

Also please be sure to create Swing components on Event Dispatch Thread . 另外,请确保在事件调度线程上创建Swing组件。 If you haven't already have read on Concurrency in Swing . 如果您尚未阅读Swing中的Concurrency

The reason I mention the above is not creating and manipulating Swing components on the EDT can cause random artifacts in the code. 我上面提到的原因不是在EDT上创建和操作Swing组件会导致代码中出现随机伪像。 ie focus is not being given etc. 即没有给予焦点等。

This code below was created to show how calling requestFocusInWindow before a component is visible will not work but calling it after its visible works as expected. 创建下面的代码是为了显示在组件可见之前调用requestFocusInWindow将如何工作,而在其可见组件正常工作之后如何调用它。

Also note that removing the SwingUtilities block will cause the requestFocusInWindow not to work as expected (ie we might be given focus or not depending on our luck :P): 还要注意,删除SwingUtilities块将导致requestFocusInWindow无法按预期工作(即,取决于运气:P,我们可能会被给予焦点或不给予焦点):

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JTextField f1 = new JTextField(10);

        JTextField f2 = new JTextField(10);

        //f2.requestFocusInWindow(); //wont work (if uncomment this remember to comment the one after setVisible or you wont see the reults)

        JButton b = new JButton("Button");

        JPanel p = new JPanel();

        p.add(f1);//by default first added component will have focus
        p.add(f2);
        p.add(b);

        frame.add(p);

        //f2.requestFocusInWindow();//wont work
        frame.pack();//Realize the components.
        //f2.requestFocusInWindow();//will work
        frame.setVisible(true);

        f2.requestFocusInWindow();//will work
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {//if we remove this block it wont work also (no matter when we call requestFocusInWindow)
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

I would suggest a read on How to Use the Focus Subsystem . 我建议阅读有关如何使用Focus子系统的内容

Often it is nice to indicate which field you want to have focus when you create the field and not separate the code by adding the request focus when the frame becomes visible. 通常,在创建字段时指明要聚焦的字段是很好的,并且当框架可见时不添加请求焦点来分离代码,这是很好的选择。

Take a look at Dialog Focus which has a solution that is also applicable in this case. 看一下Dialog Focus ,它具有在这种情况下也适用的解决方案。 Using this approach your code would look like: 使用这种方法,您的代码将如下所示:

JTextField f2 = new JTextField(10);
f2.addAncestorListener( new RequestFocusListener() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击面板时丢失焦点在JTextField上 - Lose Focus on JTextField when clicking on the panel 添加到面板时组件未出现 - Component not appearing when adding to panel 检测舞台何时再次聚焦并加载场景 - Detect when stage again in focus and scene is loaded 当CardLayout更改时,从另一个面板集中关注JTextField - Get focus on a JTextField from another panel when a CardLayout changes 在Swing中加载框架时,如何设置对JDateChooser的关注? - How to set focus on JDateChooser when frame is loaded in Swing? (辅助功能)使用“话语提示”时将自动对焦更改为其他组件 - (Accessibility) Change auto focus to other component when using TalkBack 聚焦于自定义组件时,默认按钮不监听输入键 - default button is not listen to enter key when focus in custom component 当 Tab 键没有视觉焦点指示时的焦点组件 - Focused component when tabbing has no visual indication of focus 值更改时,面板上的组件如何警告面板外的其他组件? - How can a component on a panel alert other components outside the panel when a value changes? 默认情况下,如何知道何时对父组件可见或由用户操作启动了对第一个组件的关注 - How to know when the gained focus on the first component is initiated by default when parent component became visible or by users actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM