为什么 SwingWorker#setProgress 限制为 [0, 100]?

发布于 2024-12-27 17:29:12 字数 1431 浏览 0 评论 0原文

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

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

发布评论

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

评论(4

坐在坟头思考人生 2025-01-03 17:29:12

讨论 api 设计非常有趣,但很容易猜测:-)

周末的一些随机想法:

  • SwingWorker 的基本流程模型是允许在后台执行某些操作并支持报告中间结果和最终结果/s 回到 EDT。

  • 它的目的是子类化(正如 @trashgod 已经强调的那样),同时它试图最小化这样做所需的工作:状态和进度是完全定义和实现的便利属性,意味着按原样使用。

  • 因此,API 文档规则 - 明确规定了绑定属性过程的有效值在 0 ... 100 的范围内。没有歧义,自定义实现中不允许更改任何内容。以百分比显示进度是最常见的用例,开箱即用的支持是合理的做法。

  • 为了以不同于百分比的坐标报告中间结果,预期路径是实现 process(..)。这取决于自定义实现,它到底做什么,可以触发自定义属性或直接填充视图模型,或者......无论什么

  • 最后,教程示例只是一个示例。另外,它本身是不一致的:描述中提到了方法 task.getLenghtOfTask ,但该方法并未在自定义 swingworker 中实现。我(疯狂)的猜测是,示例文本仍然基于可能支持任意值的旧版本。

回答这个问题(我的 0.02 欧元:-) - 设计没有什么不好,它是一个平衡良好的基础实现,可以立即处理常见的用例,同时足够灵活,可以使更高级的需求变得简单来实施。

一旦有一条信息可用即可更新

您就无法完全控制报告粒度进行更新:您所能做的就是定义最小块的单位,然后必须期望收到一个或多个那些最小的硬币。

Discussing api design is quite entertaining, but prone to guessing :-)

Some random thoughts at the end of the week:

  • the basic process model of a SwingWorker is to allow doing something in the background and support reporting both intermediate and end result/s back to the EDT.

  • it's intented for subclassing (as @trashgod already emphasized), at the same time it tries to minimize the effort required doing so: state and progress are fully defined and implemented convenience properties, meant to be used as-is.

  • being so, the api doc rules - which clearly states the valid values of the bound property process being in the range of 0 ... 100, incusively. No ambiguity, nothing allowed to be changed in custom implementations. Showing progress as a percentage is the most common use case, supporting that out off the box is a reasonable thing to do.

  • for reporting intermediate results in coordinates different from percentage, the intenteded path is to implement process(..). It's up to the custom implmentation what exactly it does, could be firing a custom defined property or filling a view model directly or ... whatever

  • lastly, the tutorial example is just that, an example. Plus, it's inconsistent in itself: the description mentioning a method task.getLenghtOfTask which isn't implemented in the custom swingworker. My (wild) guess is, that the example text is still based on an older version which might have supported arbitrary values.

To answer the question (my 0.02Euro cents :-) - nothing bad in the design, it's a well-balanced base implmentation which handles the common use cases out off the box and at the same time is flexible enough to make more advanced requirement simple to implement.

to update once a single piece of information is available

you can't completely control the reporting granularity: all you can do, is to define the unit of the smallest chunk and then must expect to receive one or several of those smallest coins.

甜宝宝 2025-01-03 17:29:12

他们将进度限制为 [0,100] 是否有充分的理由?

就这个问题有建设性答案而言,什么构成进步的概念可能最了解后台线程中正在建模的内容。缩放限制了必须暴露的细节量。

同时,SwingWorker打算进行子分类。没有什么可以阻止添加 setProgress(int Progress, in min, int max) 方法和随之而来的属性更改支持,但很难看到任何优势 - 接收者只需缩放它即可显示。

Is there a good reason that they limited the progress to [0,100]?

To the extent that this question has a constructive answer, the notion of what constitutes progress is probably best known to whatever is being modeled in the background thread. Scaling limits the amount of detail that must be exposed.

At the same time, SwingWorker is intended to be sub-classed. Nothing precludes adding a setProgress(int progress, in min, int max) method and the attendant property change support, but it's hard to see any advantage—the recipient would just have to scale it for display.

南薇 2025-01-03 17:29:12

1) SwingWorker#setProgress --->我将其视为百分比量,基本上范围在 0 到 100 之间,同意代数在 % 处知道负 % 或 1M,但与计算第四/第五维度相同,可以使用 Drake 方程,但可能

2) JProgressBar --->没有这种限制/限制,因为您可以编写漂亮的代码,可以在 %、Mb 之间切换 (JComboBox) 值,保持...以取得进展

1) SwingWorker#setProgress ---> I take that as percentual amount, basically is there range betweens 0 and 100, agreed that algebra knows negative % or 1M at %, but same way you can calculating fourth/fifth dimension, little bit hardier could be with Drake equation, but possible

2) JProgressBar ---> hasn't this limitations/restiction, because you can wrote nice code, where is possible to switch (JComboBox) betweens value in %, Mb, remain .... for progress

简美 2025-01-03 17:29:12

这取决于您的用例,但您也可以使用发布方法来发布进度并更新发布方法中的进度栏​​。有关示例,请参阅我对另一个SO问题的回答

It depends on your use-case, but you can also use the publish method to publish progress and update a progress bar in the publish method. See my answer on another SO question for an example

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