Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java DefaultTableModel use Vector?

I know we have to use AWT thread for all table model update operations. Under the single AWT thread, any table model will be thread-safe. Why DefaultTableModel picks thread-safe Vector as its data stucture, which slower than other data structures like ArrayList?

like image 914
user729309 Avatar asked Apr 28 '11 12:04

user729309


People also ask

What is the use of DefaultTableModel in Java?

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. The first index in the Object[][] array is the row index and the second is the column index.

What is the purpose of JTable *?

The JTable is used to display and edit regular two-dimensional tables of cells.

What is table model in Java?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);

How do you delete a row in Java?

We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.


1 Answers

Swing first appeared before Java 1.2, so before ArrayList was available. Unfortunately, the API for DefaultTableModel exposes the fact that it uses Vector, so changing it now would be backwardly incompatible.

This is exactly the kind of reason for thinking about encapsulation carefully - it lets you change the internals later on. (Admittedly getting serialization right would have been interesting, but that's a story for another day...)

like image 75
Jon Skeet Avatar answered Sep 29 '22 18:09

Jon Skeet