面板加载时关注组件 [英] Focus on component when a panel is loaded

查看:174
本文介绍了面板加载时关注组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架,我在哪里加载面板。它工作正常,但没有关注,当它加载。按下标签不起作用。我必须使用鼠标按下一个文本框。
我试过了: jtextfield1.requestFocus(); jtextfiel1.requestFocusInWindow();

我在做什么错了?



JPanel :
$ b

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

$

$ p
$ b $ p $将面板置于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);
}
};


解决方案

您应该调用 requestFocusInWindow )只有当组件在容器上可见/显示时,或者在 pack()被调用并且所有组件被添加到容器之后,不能工作。



另请确保在事件调度线程。如果您尚未阅读 Swing中的并发 。 p>

我之前提到的原因不是创建和操纵EDT上的Swing组件,可能导致代码中的随机工件。即焦点没有被赋予等等。

下面的代码是为了显示在组件之前如何调用 requestFocusInWindow 可见的将无法正常工作,但可以按照预期的方式调用它。



另外请注意,删除 SwingUtilities 块会导致 requestFocusInWindow 不按预期工作(即我们可能会被关注或不依赖于我们的运气:P):

  import javax .swing.JButton; 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
导入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(); //不会工作(如果取消注释,记得在setVisible之后注释一个或者你不会看到reults)

JButton b = new JButton(Button);

JPanel p = new JPanel();

p.add(f1); //默认情况下,第一个添加的组件将有焦点
p.add(f2);
p.add(b);

frame.add(p);

//f2.requestFocusInWindow();//wont work
frame.pack(); //实现组件。
//f2.requestFocusInWindow();//将工作
frame.setVisible(true);

f2.requestFocusInWindow(); //将工作
}

public static void main(String [] args){
SwingUtilities.invokeLater新的Runnable(){/ /如果我们删除这个块它不会工作(不管我们什么时候调用requestFocusInWindow)
@Override
public void run(){
new Test();
}
});


$ / code $ / pre

我会建议阅读如何使用焦点子系统


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. I have to use the mouse to press a textfield. I've tried: jtextfield1.requestFocus(); and jtextfiel1.requestFocusInWindow(); But it doesn't work.

What am I doing wrong?

The constructor in the 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:

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.

Also please be sure to create Swing components on Event Dispatch Thread. If you havent already have read on Concurrency in Swing.

The reason I mention the above is not creating and manipulating Swing components on the EDT can cause random artifacts in the code. i.e 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.

Also note that removing the SwingUtilities block will cause the requestFocusInWindow not to work as expected (i.e we might be given focus or not depending on our luck :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

这篇关于面板加载时关注组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆