1

Does anyone know why JTable in Java Swing use Vector as constructor params: like JTable(Vector rowData, Vector columnNames), although Vector is marked as obsolete collection. (I know there are other options too but using Vector is much more convenient). Does it have anything related to thread synchronization?

ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • 3
    When Swing was first introduced (way back when it was a separate Jar), the collections API did not exist, `Vector` was the only "list" like class available in the JDK. It's maintained as Java has a strong focus on backwards compatibility. The fact that the list is synchronised has little to do with, as Swing is not thread safe and the underlying data should not be modified outside of the context of the EDT, in fact, the fact that it is synchronised actual makes it a bad candidate for this use, as it introduces an overhead which can slow down the performance – MadProgrammer Jul 18 '14 at 00:05
  • Vector is not obsolete nor deprecated here's the documentation: http://docs.oracle.com/javase/8/docs/api/deprecated-list.html – Typo Jul 18 '14 at 00:11
  • @boolean: it is marked as obsolete collection in Netbeans 6+. For your information: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – ipkiss Jul 18 '14 at 00:21
  • And for those who voted down my question, can you explain more why my question is bad so that I can improve other questions? – ipkiss Jul 18 '14 at 00:23
  • @ipkiss well they're wrong. I can understand why it could be considered obsolete, but the fact is that the official documentation does not say that. – Typo Jul 18 '14 at 00:25
  • @ipkiss and the fact that is still used in a constructor like in the JTable class says otherwise too. If it's obsolete then this constructor also is obsolete (which it isn't) – Typo Jul 18 '14 at 00:30
  • 1
    @ipkiss there are people here who downvote anonymously because they fear retribution if they try to explain -- I'm not condoning it, just reporting something I've read here. If I had to guess about this downvote, I'd say it might be because the question could be read as one attempting to fathom a design decision by the Swing people, instead of a technical how-to-do-it question as most good SO questions are. In this case, I think the answer is clear and it shouldn't have been downvoted. – arcy Jul 18 '14 at 00:38
  • @rcook: thanks a lot for your comment. I asked because I really worry about the vector class, as in this question http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated. I'm working with Swing project, and since it is marked as obsolete in Netneans, I dont know if in the future it is removed or not. I'm using JDK 8 with Netbeans 8 and it is still marked as obsolete. But it is clear now. Thanks again. – ipkiss Jul 18 '14 at 02:02
  • I don't think either Oracle or the Open Source community would remove any class (or method) without going through the standard Java way of allowing people to prepare for it: marking it deprecated in a new version of the language, so that the compiler would notify users that they were using a feature that had been marked for removal. And they aren't going to make a change that breaks Swing applications, without letting users convert to its replacement. – arcy Jul 18 '14 at 13:21

1 Answers1

4

While Vector is neither obsolete nor deprecated, it is a legacy of the original DefaultTableModel; later, it "was retrofitted to implement the List interface." Vector is synchronized, but using it correctly for this feature effectively relies on an implementation detail. Instead, rely on the Memory Consistency Properties of classes whose methods provide a happens-before relation. For example, SwingWorker, which implements Future<T> and RunnableFuture<T>, is particularly convenient for reliably updating a table's model on the event dispatch thread. This obviates the need to synchronize the model's internal data structure(s) redundantly.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045