如何关注表中的 JTextField

发布于 2024-07-10 04:46:56 字数 654 浏览 3 评论 0原文

我正在写一个搜索& 一种电子表格程序中的替换功能。 我想要的是,如果您搜索字符串,程序会显示一个包含已找到元素的表格。

到目前为止一切顺利,但我无法让元素获得焦点,光标位于其中,因此您可以立即开始输入。

我使用自定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-07-17 04:46:56

通过一些谷歌搜索,我发现了一个论坛线程: 以编程方式开始编辑 JTable 中的单元格回答了以下想法:(

在 JTable 的子类中)

editCellAt(row,column);

requestFocus();
DefaultCellEditor ed = (DefaultCellEditor)
getCellEditor(row,column);

ed.shouldSelectCell(new ListSelectionEvent(this,row,row,true));

它会起作用吗?

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)

editCellAt(row,column);

requestFocus();
DefaultCellEditor ed = (DefaultCellEditor)
getCellEditor(row,column);

ed.shouldSelectCell(new ListSelectionEvent(this,row,row,true));

Would it work?

天荒地未老 2024-07-17 04:46:56

您是否尝试过没有 requestfocus 的 editcellat ?

还要确保您覆盖/实现返回 true

    /**
     * Returns true.
     * @param anEvent  an event object
     * @return true
     */
    public boolean shouldSelectCell(EventObject anEvent) { 
    return true; 
    }

Did you try the editcellat without the requestfocus ?

also make sure that you override/implemenet to return true

    /**
     * Returns true.
     * @param anEvent  an event object
     * @return true
     */
    public boolean shouldSelectCell(EventObject anEvent) { 
    return true; 
    }
沦落红尘 2024-07-17 04:46:56

检查您是否在自定义表格实例上启用了选择,如下所示:

table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);

设置后,通常会调用 table.editCellAt(row, col); 开始编辑。 示例:

JTable myTable = new JTable(rows, cols);
myTable.setColumnSelectionAllowed(true);
myTable.setRowSelectionAllowed(true);

以及其他需要编辑的地方,

boolean wasEditStarted = table.editCellAt(row, col);
if (wasEditStarted) {
  table.changeSelection(row, col, false, false);
}

Check if you have enabled selection on your custom table instance like below:

table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);

With this being set, generally a call to table.editCellAt(row, col); starts editing. Example :

JTable myTable = new JTable(rows, cols);
myTable.setColumnSelectionAllowed(true);
myTable.setRowSelectionAllowed(true);

and somewhere else..where edit is needed,

boolean wasEditStarted = table.editCellAt(row, col);
if (wasEditStarted) {
  table.changeSelection(row, col, false, false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文