如何关注表中的 JTextField
我正在写一个搜索& 一种电子表格程序中的替换功能。 我想要的是,如果您搜索字符串,程序会显示一个包含已找到元素的表格。
到目前为止一切顺利,但我无法让元素获得焦点,光标位于其中,因此您可以立即开始输入。
我使用自定义的 JTable
和自定义的 TableCellEditor
。 以下技巧似乎不起作用: (在自定义的 TableCellEditor
中):
SwingUtilities.invokeLater(new Runnable() {
public void run() {
my_textfield.requestFocus();
}
});
或:
my_jtable.editCellAt(0, 3);
my_jtable.requestFocus();
或
my_jtable.getEditorComponent().requestFocusInWindow();
我遗漏了什么吗? 是否有一个很好的描述(漂亮的流程图)来显示事件是如何发生的? 或者可能执行类似操作的示例代码?
I'm writing a search & replace function in a kind of spreadsheet program. What I want is that if you search for a string, the program shows a table with the element that has been found.
So far so good, but I cannot get the element to obtain the focus, with the cursor in it so you can immediately start typing.
I'm using a customized JTable
and also a customized TableCellEditor
. The following tricks do not seem to work:
(within the customized TableCellEditor
):
SwingUtilities.invokeLater(new Runnable() {
public void run() {
my_textfield.requestFocus();
}
});
or:
my_jtable.editCellAt(0, 3);
my_jtable.requestFocus();
or
my_jtable.getEditorComponent().requestFocusInWindow();
Am I missing something? Is there a good description (nice flow diagram) that shows how events take place? Or example code that might do something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过一些谷歌搜索,我发现了一个论坛线程: 以编程方式开始编辑 JTable 中的单元格回答了以下想法:(
在 JTable 的子类中)
它会起作用吗?
With some googling i found a forum thread : programmatically start editing a cell in a JTable answered with following idea:
(in a subclass of JTable)
Would it work?
您是否尝试过没有 requestfocus 的 editcellat ?
还要确保您覆盖/实现返回 true
Did you try the editcellat without the requestfocus ?
also make sure that you override/implemenet to return true
检查您是否在自定义表格实例上启用了选择,如下所示:
设置后,通常会调用
table.editCellAt(row, col);
开始编辑。 示例:以及其他需要编辑的地方,
Check if you have enabled selection on your custom table instance like below:
With this being set, generally a call to
table.editCellAt(row, col);
starts editing. Example :and somewhere else..where edit is needed,