Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Model for JTable

 
Terrence White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have noticed that most models for JTables use a multidimensional array to store the data for the table.
For example:

This array cannot be resized automatically.
Is there an easy way to use one of the classed in the Collections Framework
to store the data in the model so that I can add rows to the table without worrying about going out of bounds with my index?
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.swing.table.DefaultTableModel takes either a multidimensional array or a Vector (of Vectors, each being a row) of data among other things.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one word of advise, using Vector is easy, but not a good idea. Use ArrayList, Vector was introduced in jdk 1.1 maybe? you can see that it doesn't have the standard methods in the new data strcuture class in jdk2.
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adrian Yan:
Just one word of advise, using Vector is easy, but not a good idea. Use ArrayList, Vector was introduced in jdk 1.1 maybe? you can see that it doesn't have the standard methods in the new data strcuture class in jdk2.

Vector was introduced in good-old Java 1.0. Since Java 1.2 Vector has extended AbstractList, so it compatable with the rest of the java.util package. The major complaint against Vector is that all its methods are synchronized, which increases the overhead of calling them. I would use the API-provded javax.swing.table.DefaultTableModel unless profiling one's program reveals that using DefaultTableModel is taking an extraordinary amount of time due to synchronization. Then I would look at implementing my own version of javax.swing.table.TableModel using ArrayLists or some sort of paging mechanism.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic