JTable:选择选项卡上的下一个单元格,但第一个焦点选择同一单元格,而不是下一个单元格

发布于 2024-12-08 04:48:30 字数 1222 浏览 1 评论 0原文

我想要:

  • 使用按钮向 JTable 添加一行
  • 当创建该行时,为其提供焦点并开始编辑第一列
  • 当用户按 Tab 时,选择下一列。

对于“添加”按钮,我这样做:

add_button.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        int row = table_model.addRow();
        table.setRowSelectionInterval(row, row);
        table.scrollRectToVisible(table.getCellRect(row, 0, true));
        table.editCellAt(row, 0);
        table.transferFocus();
        did_add = true;
    }
});

这确实会导致表格单元格获得焦点并开始编辑。为了确保按 Tab 键选择下一个单元格,我有这样的:

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0, false), "selectNextColumnCell");

这是我的问题:

  • 如果我之前选择了任何单元格,那么这将非常有效。我可以编辑第一列,然后按 Tab 键,这会导致选择下一列。
  • 但是,如果此表是新创建的,或者该表外部的某些小部件先前已获得焦点,并且我单击“添加”按钮,则会发生这种情况:
    • 第一列已正确选择且可编辑
    • 当我按 Tab 时,通常会聚焦同一个单元格
    • 我必须再次按 Tab 才能转到下一列
    • 有时,第一个选项卡实际上会转到单元格 0,0

因此,虽然单元格已获得焦点并正在编辑,但表格的焦点有些问题,因为下一个单元格是不正确的。

我尝试了一些涉及将焦点设置到表格的解决方法,但似乎都不起作用(requestFocus、requestFocusInWindow、设置表格选择等)。

知道如何解决这个问题吗?

I would like to:

  • Have a button add a row to a JTable
  • When that row is created, give it focus and start editing the first column
  • When the user presses Tab, select the next column.

For the Add button, I do this:

add_button.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        int row = table_model.addRow();
        table.setRowSelectionInterval(row, row);
        table.scrollRectToVisible(table.getCellRect(row, 0, true));
        table.editCellAt(row, 0);
        table.transferFocus();
        did_add = true;
    }
});

This does indeed cause the table cell to take focus and start editing. To ensure that pressing Tab selects the next cell, I have this:

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0, false), "selectNextColumnCell");

Here's my problem:

  • If I have made any cell selections previously, this works perfectly. I can edit the first column, and press Tab, and that causes the next column to be selected.
  • However if this table is newly created or some widget outside of that table has previously had focus, and I click my Add button, then this happens:
    • The first column is selected properly and is editable
    • When I press Tab, usually the same cell is focused
    • I have to press Tab a second time to go to the next column
    • Sometimes, that first tab actually goes to cell 0,0

So, it seems that although the cell is focused and being edited, something is awry with the table's focus, because it is incorrect about which cell is next.

I have tried a few work-arounds involving setting the focus to the table, but none seem to work (requestFocus, requestFocusInWindow, setting the table selection, etc.).

Any idea how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雨后咖啡店 2024-12-15 04:48:30

发生这种情况是因为通过设置表的行选择更改列选择:启动时未选择它,然后导航到下一列(也称为:按 Tab 键移出已编辑的内容)将其设置为第一列。要解决此问题,请设置列选择和行选择

   table.setRowSelectionInterval(row, row);
   table.setColumnSelectionInterval(0, 0);

That happens because the column selection is not changed by setting the table's row selection: on startup it's unselected, then navigating to the next column (aka: tabbing out off the edited) sets it to the first column. To solve, set the column selection as well as the row selection

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