使用 java DefaultCellEditor 在 JTable 中获得正确的编辑行为
您可以通过单击或使用光标键/选项卡进入 Jtable 中的单元格。对于 defaultCellEditor 和 JtextField,如果您使用光标键进入,插入符号将放置在现有文本字段的末尾,而如果您双击该字段,它将突出显示最后一个单词。
虽然电子表格(例如 Open Office Calc)的工作方式似乎与双击相同,但如果您按 Tab 键进入字段并开始编辑,则该字段将被清除,并且按下的第一个字符将成为该字段中的第一个值,依此类推。
我希望我的应用程序以与电子表格相同的方式工作。通过子类化 DefaultCellEditor 并添加,
final Caret caret = editField.getCaret();
caret.setDot(0);
editField.setText("");
我可以让它在按 Tab 键时按照我想要的方式工作,但它也会在双击时清除我不想要的字段。
那么我如何确定单元格编辑是否是由键盘或鼠标触发的?
You can go into a cell in a Jtable either by clicking on it, or by going into it using cursor keys/tabs. With defaultCellEditor and a JtextField if you go in using cursor keys the caret is put at the end of the existing text field, whereas if you double click the field it will highlight the last word.
Whereas spreadsheets seem to work (such as Open Office Calc) the same way for a double clcking, but if you tab into the field and start editing the field is cleared and the first character pressed becomes the first value in the field and so on.
I want my app to work the same way as the spreadsheet. By subclassing DefaultCellEditor and adding
final Caret caret = editField.getCaret();
caret.setDot(0);
editField.setText("");
I can get it to work how I want when tabbing but it also clears the field on double click which I dont want.
So please how can I determine if cell editing has been triggered by keyboard or mouse ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也重写 isCellEditable(EventObject anEvent) 方法。
这样您就可以捕获将触发(或不触发)表格版本的事件并按照您想要的方式行事
override the isCellEditable(EventObject anEvent) method too.
So that you can capture the event which is going to trigger (or not) the table edition and act the way you want