在标签按下的Jtable中将焦点从一个单元格移动到另一个单元格 [英] Shifting the focus from one cell to another in Jtable on tab press

查看:163
本文介绍了在标签按下的Jtable中将焦点从一个单元格移动到另一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:

在我的桌面应用程序中,我加载一个Jtable,当处于编辑模式时,如果我按Tab键我需要焦点单元格到下一个单元格。

In my desktop application i load one Jtable and when in edit mode if i press tab i need the focus of the cell on to the next cell.

问题:
当我编辑单元格的值然后按Tab键时焦点丢失了。我在网上做了一些搜索,我发现它发生了,因为在每个Tab上按下Jtable重新加载。

Problem: When i am editing the value of a cell and then when i press Tab the focus is lost. I did some search on the net and i found that it happens because on each Tab press the Jtable reloads itself.

可能的解决方案
我想到的一个解决方案是获取我正在工作的单元格的索引,在全局变量中相同,然后在Tab按下我可以得到下一个单元格的索引并将焦点设置在该单元格上。
不知何故它不起作用。

Possible Solution One solution that i was thinking of is to get the indices of the cell i am working in, same it in a global variable and then on Tab press i can get the indices of the next cell and set focus on that cell. Somehow it didn't work.

请建议。

提前致谢..

推荐答案

我认为我们用自定义击键在tabes中实现 InputMap & ActionMap

Off the top of my head, I think we overcame this with a custom keystroke implementation in the tabes InputMap & ActionMap.

我们使用的实现允许我们执行连续编辑,即当用户按下时输入或制表符,我们移动到下一个可编辑单元格并开始编辑

The implementation we use allows us to perform "continuous" editing, that is, when the user presses enter or tab, we move to the next editable cell and start editing

InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();

KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);

Action moveNextCellAction = am.get(im.get(tabKey));

ContinousEditAction continousEditAction = new ContinousEditAction(table, moveNextCellAction);

im.put(tabKey, "Action.tab");

am.put("Action.tab", continousEditAction);

ContinousEditAction 负责寻找下一个可编辑的细胞。基本上,当触发操作时,您可以通过 JTable.getEditingRow &来评估当前单元格。 JTable.getEditingColumn 方法(您还希望通过 JTable.isEditing 检查表是否为编辑模式,否则您需要使用 JTable.getSelectedRow & JTable.getSelectedColumn - 实际上你可能会这样做,但这个我是如何处理这个问题的。)

The ContinousEditAction is responsible for finding the next editable cell. Basically when the action is fired, you take stock of the current cell via JTable.getEditingRow & JTable.getEditingColumn methods (you also want to check that the table is edit mode via JTable.isEditing, otherwise you need to use JTable.getSelectedRow & JTable.getSelectedColumn - in fact you might get away with doing just this, but this is how I approached the problem).

从那里,你想走单元格直到你找到一个可编辑的单元格。

From there, you want to walk the cells until you find a cell that is editable.

基本上,你要检查当前行的末尾,然后移动到下一行直到不再存在行,这取决于你想要做什么,你可以选择循环回到开始表格(单元格0x0)然后走到你到达当前位置。

Basically, you want to check to the end of the current row, then move to the next until no more rows exist, depending on what you want to do, you may choose to loop back around to the start of the table (cell 0x0) and walk it until you reach your current position.

小心,如果你不小心,你可以最终连续循环:P 。

Be careful, you can end up in a continuous loop if you're not careful :P.

如果找不到任何可编辑的单元格,您可能只希望使用 JTable.setRowSelectionInterval & JTable.setRowSelectionInterval ,另外你可以调用 JTable.editCellAt(nextRow,nextCol)

If you don't find any editable cells, you may simply wish to select the next available cell using JTable.setRowSelectionInterval & JTable.setRowSelectionInterval, other wise you can call JTable.editCellAt(nextRow, nextCol)

但这一切都取决于你想要实现的目标。

But this all comes down to what it is you want to achieve.

此外,你可以将相同的想法应用于回车键; )

Also, you can apply the same idea to the enter key ;)

这篇关于在标签按下的Jtable中将焦点从一个单元格移动到另一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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