Java Swing:重新计算 JTable 中的值、使用 TableModel 或编写自定义编辑器?

发布于 2024-08-22 08:55:10 字数 592 浏览 3 评论 0原文

我使用 JTable 来显示来自流数据源的数据。

每个数据点都表示为 X 类型的对象,该对象有一个感兴趣的字段,我们将其称为 valueField。我的 TableModel 接口的实现有一个以 X.getId() 为键的对象 X 的 HashMap。

GUI 用户可以使用 JSpinner 设置为相关列中每个单元格的自定义编辑器来更改值。

但是,该值还取决于其他行中 X.valueField 的值。

例如,我的表代表 3 个对象 X1、X2 和 X3。它们的关系是:

X1.valueField = X2.valueField + X3.valueField

如果用户更改 X1,则 X2 和 X3 之一保持不变(取决于业务逻辑),而另一个值发生更改以保持关系一致。类似地,用户也可以改变X2或X3。

我应该将关系封装在 TableModel 的实现中,还是应该扩展自定义表编辑器以考虑值可以更改的方式?

我认为这两种方法都可行,但我不确定哪种方法是正确的。 Sun 和其他论坛的文档建议扩展自定义编辑器来控制字段的值,但它没有考虑该值与底层数据模型的其他成员的关系。

任何想法都非常感激。

00冲

I use a JTable to display data sourced from a streaming data feed.

Each data point is represented as an object of type X, which has one field of interest, lets call it valueField. My implementation of the TableModel interface has a HashMap of objects X keyed on X.getId().

The users of the GUI are able to change the values using a JSpinner set as the custom editor for each cell in the relevant columns.

However, the value is also dependent on values of X.valueField in other rows.

For example, my table represents 3 objects X1, X2 and X3. Their relation is:

X1.valueField = X2.valueField + X3.valueField

If the user changes X1, one of X2 and X3 is kept constant (depending on business logic), and the other value is changed to keep the relationship consistent. Similarly, the user may also be able to change X2 or X3.

Should I encapsulate the relationship in my implementation of the TableModel, or should I extend my custom table editor to take into account the ways the value can change?

I think both approaches would work, but I am not sure which one is the correct one to take. The documentation from Sun and other forums suggests extending the custom editor to control the value of the field, but it doesn't take into account the relationship of that value with other members of the underlying data model.

Any thoughts much appreciated.

00rush

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

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

发布评论

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

评论(1

書生途 2024-08-29 08:55:10

我建议,如果用户执行的 mods 是永久性的,即一旦制作了 mod,支持数据就会更新 - 那么让表模型维护修改后的数据是正确的。另一方面,如果存在编辑模式,用户可以在其中处理数据并在完成后提交,我建议编辑者维护 mods,直到用户触发提交过程。

I would suggest that if the mods that the users perform are permanent, ie once the mod is made, the backing data is updated - then having the table model maintain the modified data is correct. On the other hand, if there is an edit mode, where the user can work with the data and commit once finished, I would suggest that the editor maintain the mods until such a time as the user triggers the commit process.

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