使用 java DefaultCellEditor 在 JTable 中获得正确的编辑行为

发布于 2024-09-28 11:26:30 字数 495 浏览 3 评论 0原文

您可以通过单击或使用光标键/选项卡进入 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 技术交流群。

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

发布评论

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

评论(1

可遇━不可求 2024-10-05 11:26:30

也重写 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

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