JOptionPane 元素之间的交替焦点
我试图显示一个带有 JTextField
的 JOptionPane
,它具有初始焦点,并且一旦用户按下 ENTER,我希望它执行一个操作在文本字段中输入的文本。
我做了一些广泛的搜索,但没有找到任何可以帮助我的东西。我将在这里说明到目前为止我所管理的内容:
这就是我所得到的,
Object[] options = {"Option1",
"Option2"};
Object[] message = new Object[2];
message[0] = "Type in the number of the incident:";
JTextField incidentNumberTextField = new JTextField();
message[1] = incidentNumberTextField;
int n = JOptionPane.showOptionDialog(frame,
message,
"Open incident",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
message[1]);
if (n == -1) {
return;
}
到目前为止它运行良好。当对话框出现时,焦点位于文本字段上。但是,当我输入文本并按 Enter 时,它希望它自动触发“Option1”按钮。
我已经尝试过侦听器,但似乎我无法从其中访问非最终数据(即文本字段)。
I am trying to show a JOptionPane
with a JTextField
, which has initial focus, in it and, as soon as the user press ENTER, I want it to perform an action with the text entered in the text field.
I did some extensive search and I failed to find anything that could help me on that. I will state here what I managed so far:
This is what I got
Object[] options = {"Option1",
"Option2"};
Object[] message = new Object[2];
message[0] = "Type in the number of the incident:";
JTextField incidentNumberTextField = new JTextField();
message[1] = incidentNumberTextField;
int n = JOptionPane.showOptionDialog(frame,
message,
"Open incident",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
message[1]);
if (n == -1) {
return;
}
It works fine so far. When the dialog shows up, the focus is on the text field. However, when I type in the text and press Enter, it would like it to automatically trigger the "Option1" button.
I have tried listeners, but it seems I can not access data that is not final - i.e. the text field - from inside it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您有几个问题需要相互斗争来解决:-)
最后一个可以通过 JTextField 的自定义子类来解决,就像 BasicOptionPaneUI 用于 inputDialog 一样,它显示在最后 - 有用选项面板仅当前两个问题得到解决时,上下文才有效。我还没有看到完全令人满意的解决方案:将“消息”的概念与“选项”的概念混合在一起会使 optionPane 混淆,不设置 rootpane 的默认按钮。因此,最后,您可能最好不要使用第一个技巧,坚持使用“选项”概念,然后通过在字段的 addNotify 中请求转移来欺骗焦点。
自定义 JTextField 不占用某些 keyStrokes。它称为 MultiplexingTextField,并传递 keyStrokes 的处理(如果配置为这样做):
在受控环境中使用:
Basically, you have several problems which are fighting with each other to get solved :-)
the last can be solved by a custom subclass of JTextField, just as BasicOptionPaneUI uses for the inputDialog, it's shown at the end - useful in the optionPane context only if the first two are solved. Which I haven't seen a fully satisfactory solution for: mixing the concept of "message" with the concept of "options" confuses the optionPane into not setting the rootpane's default button. So at the end, you might be better of to not use that first trick, stick with the "options" notion and then tricks the focus via requesting a transfer in the field's addNotify.
The custom JTextField which doesn't eat certain keyStrokes. It's called MultiplexingTextField and passes on processing of keyStrokes if configured to do so:
usage in controlled environment: