I am building the simple program where i have pre filled books with name , author , price in the Jtable so that i can show them in list.
Initially i created 2d array and it worked ok.
But now i want my program to dynamically add or delete in that list.
but array can’t do that so which is best way to do that. with example will be good
Personally I would suggest that you define your own implementation of the TableModel interface (probably by subclassing AbstractTableModel). Then, as Andrezj suggests, you can use an ArrayList internally to hold the data. See this tutorial on using the
JTable. It’s more work, but it gives you more control over the data.If you don’t want to do that, then you can use the DefaultTableModel, which allows you to add rows dynamically, but it’s not type safe, and not threadsafe if you use
Vectors to provide the data. In fact, I’m not sure exactly what happens when you supply a row in the form of aVectorand then change thatVectorafter it’s been added to the table. If theDefaultTableModelmakes it own (deep?) copy then it might be a non-issue, but I don’t know if it does that. Perhaps someone knowledgable can comment