如何跨表格单元格和文本字段镜像编辑

发布于 2024-08-17 19:59:22 字数 512 浏览 8 评论 0原文

所需的行为类似于选择给定单元格时 Excel 中提供的镜像文本编辑字段,从而允许有更多空间来查看单元格的内容。我有一个包含 5 列和 n 行的 JTable。第 2 列包含可以任意长的表达式,因此我想提供一个单独的 JTextField 来编辑每行表达式单元格的内容。其他字段可直接在表中编辑。然而,当用户单击第 2 列中的字段时,我想将它们发送到文本字段。单元格中预先存在的任何内容都应显示在文本字段中,并且文本字段中的其他编辑应反映在表格单元格中。同样,如果有人双击单元格并直接编辑它,我希望这些更改反映在文本字段中。因此,用户可以选择在任一空间中进行编辑,并且两者都会更新。理想情况下,它们会在每次击键时更新,但按回车键更新也是可以接受的。

到目前为止,我已经有了 JTable、TableModel、TableModelListener、JTextField、ListSelectionListener 和 AbstractAction,它们一起工作来提供上述的大部分功能。我缺少对文本字段的直接表格单元格编辑和每次按键更新的反映。

他们的想法是如何最好地构建这种行为?

The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.

So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.

Are their ideas on how best to construct this behavior?

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

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

发布评论

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

评论(1

澜川若宁 2024-08-24 19:59:22

好吧,如果您想从表格单元格获取数据,那么您可以将代码添加到 TableModel 的 setValueAt() 函数中,该函数应该在用户更改内容时运行在可编辑单元格中。我不认为这会更新每次击键。

如果要将数据文本框移动到表格单元格,请使用如下代码,

myJTextField.getDocument().addDocumentListener(new MyDocumentListener());

其中MyDocumentListenerjavax.DocumentListener的实现。 swing.event.DocumentListener 接口

将为您提供从框到表的每次击键更新。但反过来说就有点棘手了。

您可以通过两种方法来执行此操作:

1)向表中添加一个按键侦听器,当用户开始键入时检查以查看哪个表元素处于活动状态,并在键入时拦截击键。不过,这有点混乱。

2)另一种选择可能是尝试获取或替换表正在使用的组件,以实际让用户进行更改。我认为,如果您深入研究,JTable 实际上允许您更改编辑器组件。

Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.

If you want to move data from the textbox to the table cell use code like this

myJTextField.getDocument().addDocumentListener(new MyDocumentListener());

Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface

That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.

There are two ways you might be able to go about doing it

1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.

2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.

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