Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualizing a Set<T> with JTable in Java Swing

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.

like image 466
Konrad Reiche Avatar asked Mar 31 '26 18:03

Konrad Reiche


2 Answers

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.

like image 141
Jim Avatar answered Apr 02 '26 10:04

Jim


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.

like image 25
jzd Avatar answered Apr 02 '26 08:04

jzd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!