开始编辑 JTable Cell 时选择所有数据
我正在尝试制作一个像 Excel 一样工作的表格。这意味着,当用户开始将数据插入单元格时,单元格中的内容将被选择并由插入的新数据更改。
I'm trying to make a table that works like Excel. Meaning, when a user starts to insert data into the cells the content into them is selected and changed by the new data inserted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以为表格创建自定义 TableCellEditor。该类将有一个
TextField
的实例变量,我们将其称为textField
。那么 getTableCellEditorComponent 方法可能如下所示:You can create a custom TableCellEditor for your table. This class will have an instance variable of a
TextField
, lets call ittextField
. Then thegetTableCellEditorComponent
method could look like this:如果表中只有字符串数据并且只需要一个编辑器,那么创建自定义编辑器效果很好。但是,如果您有多种不同类型的数据,例如字符串、整数、双精度、货币、百分比等,它们都使用 JTextField 作为编辑器,那么您需要创建多个自定义编辑器。
您可以阅读表格全选编辑器 另一种可能的解决方案。
Creating a custom editor works fine if you only ever have String data in the table and only need a single editor. However, if you have multiple different types of data, like String, Integer, Double, currencies, percentages etc which all use a JTextField as an editor then you need to create multiple custom editors.
You can read up on the Table Select All Editor for another possible solution.
请注意,还有另一种可能性,您可以覆盖 JTable#prepareEditor,如下所示:
Note that there is also another possibility, you can override JTable#prepareEditor like the following:
当通过单击鼠标开始编辑时,上述解决方案不起作用。
对于某些人来说,解决方案是在 invokeLater() 中调用 selectAll() ,以便在分派鼠标事件后选择文本,但这对我不起作用(可能是因为我正在使用 Substance 外观和感觉? )
Swing 内部稍后获取 mouseReleased() 事件并再次更改插入符号,如以下堆栈跟踪所示:
这是我的解决方案:
监听插入符号位置的变化,并且在单元格编辑开始后第一次选择从所有选定变为未选定时,再次调用 selectAll()。插入符侦听器可以通过自定义单元格编辑器安装,如下所示,或者在自定义 JTable 中重写的 editCellAt() 方法中安装。
The solution above doesn't work when editing is started by a mouse click.
For some people the solution is to call selectAll() in an invokeLater() so that the text gets selected after the mouse events have been dispatched, but this isn't working for me (possibly because I'm using Substance look and feel?)
Swing internals get a mouseReleased() event later and change the caret again, as shown in this stack trace:
Here is my solution:
Listen for caret position changes, and the first time the selection goes from all selected to none selected after cell editing has started, call selectAll() again. The caret listener can be installed by a custom cell editor as shown here, or in an overridden editCellAt() method in a custom JTable.
如果您的目标是在编辑开始时清空单元格,则无需使用
selectAll()
。只需将值设置为null
。实现示例:
(覆盖
DefaultCellEditor
中的getTableCellEditorComponent()
)If your objective is to empty the cell when editing begins, no need to use
selectAll()
. Simply set value tonull
.Implementation example:
(overriding
getTableCellEditorComponent()
inDefaultCellEditor
)你应该看看 extJS。不过,有一个相当陡峭的学习曲线..
You should look at extJS. There is a pretty steep learning curve, though..