3

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

4 Answers4

3

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.

Jim
  • 22,354
  • 6
  • 52
  • 80
2

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • 1
    But which function is called in JTable to refresh the whole model? SetValueAt uses only a specific data element, also fireTableCellUpdated(row, col);. – Konrad Reiche Apr 07 '11 at 15:41
  • @Platz, You would refresh your model and call model.fireTableDataChanged(); or you would just build a new model and call setModel() on JTable. – jzd Apr 07 '11 at 15:46
0

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).

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

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.

Yishai
  • 90,445
  • 31
  • 189
  • 263