将焦点设置为文本字段 [英] Setting the focus to a text field

查看:121
本文介绍了将焦点设置为文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在NetBeans中开发的应用程序,我想在面板显示时将焦点设置为某个 jTextField 。我读了一些帖子,并尝试过各种方法,但没有工作。其中一个主要问题是放置所需代码的位置,我相信我的情况是 this.txtMessage.requestFocusInWindow();



有一些帖子指出使用Window Listener,但是由于netbeans已经生成了GUI,我看不到如何实现接口,因为我不能编辑创建jPANEL的代码等等。令人沮丧,我真的不相信这应该是困难的。

就像一个测试,我在面板上的一个按钮上添加了 requestFocusInWindow(); 集中于所需的输入。

解决方案

我有一个类似的场景,当面板显示时需要将焦点设置在面板中的文本框。面板在应用程序启动时加载,所以我无法在构造函数中设置焦点。由于该面板没有被加载或被重点放在显示,这意味着我没有事件来解决焦点要求。



为了解决这个问题,我添加一个全局方法给我的主,在面板上调用文本区域的 requestFocusInWindow()。我把这个调用放到显示面板的按钮中,在调用show之后显示。这意味着将显示面板,然后在显示面板之后文本区域被分配焦点。另外,你可以通过在设计视图中右键点击对象并选择自定义代码来编辑大部分自动生成的代码,但是我希望这是有意义的,也是有帮助的。不要认为它允许你编辑面板。

I have an application developed in netbeans and I want to set the focus to a certain jTextField when a panel is displayed. 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();

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.

Just as a test I added the 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. I put the call to the global method in the button that showed the panel, after the call to 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.

这篇关于将焦点设置为文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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