可视化集合在 Java Swing 中使用 JTable
我想可视化一组线程,例如:Set
。我选择Set
,因为 JVM 中的每个线程都是唯一的。选择使用 Java Swing 中的组件 JTable 显示它时,我遇到了一些问题。
我需要实现一个 TableModel。 TableModel 依赖 getValueAt(int row, int col)
或 setValueAt(Object o, int row, int col)
来传播更改的值。
但是如何使用 Set 作为数据模型来实现这些方法呢?对于列表,我会说行=列表索引,但是对于集合,我无法对元素顺序做出假设。
I would like to visualize a Set of Threads, something like: Set<ThreadInfo>
. I choose Set
, as every Thread in the JVM is unique. With the choice of displaying it with the component JTable in Java Swing I am facing some issues.
I need to implement a TableModel. TableModel relies on getValueAt(int row, int col)
or setValueAt(Object o, int row, int col)
in order to propagate changed values.
But how do I implement these methods using a Set as the data model? With a list I would say row = list index, but with a set I cannot make assumptions about the elements order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会坚持使用列表,因为可靠的排序对于表的支持相当重要。如果失败,您可以使用排序集,例如 TreeSet< /a>.
I would stick with using a List since reliable ordering is fairly important in the backing for a table. Failing this you could use a sorted set such as a TreeSet.
确定您希望线程如何排序,并在正确排序的列表中创建该集合的副本。使用该列表作为模型。当集合更改时,使用新排序的数据刷新您的列表。
Determine how you want the threads ordered and create a copy of the set in a list that is ordered correctly. Use that list for the model. When the set changes refresh your list with a newly ordered data.
Set不提供通过索引获取Object的方法。
您应该使用列表来做您想做的事情。
或者迭代元素,直到索引通过参数传递到“int row”(避免此解决方案)。
Set does not provide a method to get an Object through a index.
You should use a List to do what you want.
Or iterate over the elements until the index passed by parameter into the 'int row' (Avoid this solution).
表本质上是显示列表,底层数据模型通常是向量的向量或 Object[][] 数组。因此,在这种情况下,您需要将 Set 转换为 List(因此您需要两个保持同步的数据结构),并且需要合理的随机访问 - 小 Set 可能可以迭代并查找内容,但任何重要的大小将成为一个问题,因为 JTable 在某些情况下会经常询问。
A table is inherently displaying a list, the underlying data model is typically a Vector of Vectors or an Object[][] array. So in this case, you need to convert your Set into a List (so you need two data structures that stay in sync), and you need reasonable random access - a small Set may be fine to iterate over and find things, but anything of significant size is going to be an issue, as the JTable will ask often under certain circumstances.