[英]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.