简体   繁体   English

将焦点设置到文本字段

[英]Setting the focus to a text field

I have an application developed in netbeans and I want to set the focus to a certain jTextField when a panel is displayed.我有一个在 netbeans 中开发的应用程序,我想在显示面板时将焦点设置为某个jTextField I have read a number of post and have tried various methods but non have worked.我已经阅读了许多帖子并尝试了各种方法,但都没有奏效。 One of the main issues is where to place the required code, which I believe in my case is this.txtMessage.requestFocusInWindow();主要问题之一是在哪里放置所需的代码,我相信在我的情况下是this.txtMessage.requestFocusInWindow();

There are some posts that indicate using a Window Listener, however as netbeans has generated the GUI, I cannot see how to implement the interfaces as I cannot edit the code that creates the jPANEL etc. The whole thing is very frustrating and I really do not believe that this should be that difficult.有一些帖子表明使用窗口侦听器,但是由于 netbeans 生成了 GUI,我无法看到如何实现接口,因为我无法编辑创建 jPANEL 等的代码。整个事情非常令人沮丧,我真的不相信这应该是那么困难。

Just as a test I added the requestFocusInWindow();就像测试一样,我添加了requestFocusInWindow(); to a button on the panel and it did set the focus to the desired input.到面板上的一个按钮,它确实将焦点设置为所需的输入。

I have had a similar scenario where I needed to set the focus on a text box within a panel when the panel was shown.我有一个类似的场景,当面板显示时,我需要将焦点设置在面板内的文本框上。 The panel was loaded on application startup, so I couldn't set the focus in the constructor.该面板是在应用程序启动时加载的,所以我无法在构造函数中设置焦点。 As the panel wasn't being loaded or being given focus on show, this meant that I had no event to fire the focus request from.由于面板没有被加载或被赋予焦点显示,这意味着我没有触发焦点请求的事件。

To solve this, I added a global method to my main that called a method in the panel that invoked requestFocusInWindow() on the text area.为了解决这个问题,我在 main 中添加了一个全局方法,该方法调用面板中的一个方法,该方法在文本区域上调用requestFocusInWindow() I put the call to the global method in the button that showed the panel, after the call to show.在调用 show之后,我将调用全局方法放在显示面板的按钮中。 This meant that the panel would be shown and then the text area assigned the focus after showing the panel.这意味着将显示面板,然后在显示面板后文本区域分配焦点。 Hope that makes sense and helps!希望这是有道理的并有所帮助!

Also, you can edit most of the auto-generated code by right clicking on the object in design view and selecting customize code, however I don't think that it allows you to edit panels.此外,您可以通过右键单击设计视图中的对象并选择自定义代码来编辑大部分自动生成的代码,但是我认为它不允许您编辑面板。

I'm not sure if I'm missing something here, but there's no reason why you can't add a listener to your panel.我不确定我是否在这里遗漏了什么,但是没有理由不能向面板添加侦听器。

In Netbeans, just hit the "Source" button in the top left of the editor window and you can edit most of the code.在 Netbeans 中,只需点击编辑器窗口左上角的“源代码”按钮,您就可以编辑大部分代码。 The actual layout code is mostly locked, but you can even customize that if you need to.实际的布局代码大多是锁定的,但您甚至可以根据需要对其进行自定义。

As far as I'm aware, txtMessage.requestFocusInWindow() is supposed to set up the default focus for when the window is displayed the first time.据我所知, txtMessage.requestFocusInWindow()应该为第一次显示窗口时设置默认焦点。 If you want to request the focus after the window has been displayed already, you should use txtMessage.requestFocus()如果你想在窗口已经显示后请求焦点,你应该使用txtMessage.requestFocus()

For testing, you can just add a listener in the constructor:为了进行测试,您可以在构造函数中添加一个侦听器:

addWindowListener(new WindowAdapter(){ 
  public void windowOpened( WindowEvent e){ 
    txtMessage.requestFocus();
  } 
}); 

In a JFrame or JDialog you can always overwrite the setVisible() method, it works well.在 JFrame 或 JDialog 中,您始终可以覆盖 setVisible() 方法,它运行良好。 I haven't tried in a JPanel, but can be an alternative.我还没有在 JPanel 中尝试过,但可以作为替代方案。

@Override
public void setVisible(boolean value) {
    super.setVisible(value);
    control.requestFocusInWindow();
}

I have toyed with this for forever, and finally found something that seems to always work!我一直在玩这个,终于找到了似乎总是有效的东西!

textField = new JTextField() {

    public void addNotify() {
        super.addNotify();
        requestFocus();
    }
};

我通过在 textField 和窗口中的请求焦点上设置 AncesterAdded 事件来做到这一点。

For me the easiest way to get it to work, is to put the component.requestFocus();对我来说,让它工作的最简单方法是放置component.requestFocus(); line, after the setVisible(true);行,在setVisible(true); line, at the bottom of your frame or panel constructor.行,在框架或面板构造函数的底部。

This probably has something to do with asking for the focus, after all components have been created, because creating a new component, after asking for the focus request, will make your component loose te focus, and make the focus go to your newly created component.这可能与请求焦点有关,在创建了所有组件之后,因为在请求焦点请求之后创建一个新组件会使您的组件失去焦点,并使焦点转到您新创建的组件上. At least, that's what I assume.至少,这是我的假设。

None of the above worked for me, because my window is a JPopupMenu .以上都不适合我,因为我的窗口是JPopupMenu

What did work was this:什么工作是这样的:

addAncestorListener(new AncestorListener() {
    @Override
    public void ancestorAdded(AncestorEvent ae) {
        myEdit.requestFocus();
    }

    // ... other ancestor listener methods
}

If you create your GUI with Netbeans , you can also insert some self written code.如果您使用Netbeans创建您的GUI ,您还可以插入一些自己编写的代码。 Just select an element (maybe the button, panel or the window) and use the "Code"-tab in the "Properties"-dialog.只需选择一个元素(可能是按钮、面板或窗口)并使用“属性”对话框中的“代码”选项卡。

There you can insert Pre- and Post- code for various parts of the creation process.在那里,您可以为创建过程的各个部分插入代码和代码。

I think the "After-All-Set-Code" field of the window is a good place for your code, or you could bind it to the event ("Properties"-dialog -> "Events") " componentShown " of the text field / panel.我认为窗口的“After-All-Set-Code”字段是您代码的好地方,或者您可以将其绑定到文本的事件(“属性”-对话框 ->“事件”)“ componentShown ”场/面板。

这是一个简单的:

textField.setFocus();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM