• Post Reply Bookmark Topic Watch Topic
  • New Topic

is possible to add Arraylist to JTable?  RSS feed

 
vijay Panner
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

is possible to add array list directly to table as data column in java swing?. if yes, please show an example.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array list holds data. JTable is a visual component. So the answer is no.

The JTable gets all its data from the underlying table model. You can create a model from your list and then set the model to the table
More on JTable here http://download.oracle.com/javase/tutorial/uiswing/components/table.html

Your row count will be the number of elements in the list
Each cell value will be the list element at the corresponding row index
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay: Check out TableModel, AbstractTableModel and DefaultTableModel.
Data displayed by a JTable are stored in a TableModel that is often passed
to the table like this - JTable aTable = new JTable(aTableModel). You can
implement your TableModel to store any kind of Object, including ArrayList,
or even an ArrayList[]. When it displays that data, JTable will ask the model
for each row/column value by using getValueAt(row, column) which you must
provide in your TableModel implementation. I hope this helps.

Jim ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!