Reputation: 59
I am having a problem initializing a JTable from a FOR with BD or an array.
My problem is the example tableFilterDemo.java, I need the funcionality of this, but when I want load data of my BD or an arraylist I have the problem.
I need load the array of objects with a FOR getting all the lines of file or rows of table
private Object[][] data = {
{ "Mary", "Campione", "Snowboarding"},
{ "John", "guifru", "skyiin"},};
Upvotes: 0
Views: 4611
Reputation: 324197
You can always use the DefaultTableModel which uses a Vector of Vectors to hold the data in the TableModel. So for each row in the table you create a Vector and add each column to the Vecter. Then you add the row Vector to a second Vector. This way you don't need to hardcode the size of the Arrays in advance. Table From Database shows how you can use this approach.
Or you can always use a custom TableModel if you want to display custom Ojects that are stored in an ArrayList. The Row Table Model provides some general support for storing objects in an ArrayList. You would also want to look at the BeanTableModel
for a full implementation and a further example of how to do a custom implementation.
Upvotes: 4
Reputation: 2408
I'm not sure that I have understood your problem. Anyway, if your table have 3 columns and you have defined the table model as defined here, you have to itrate your array and put the values in the table in this way:
for(int i=0; i < data.length; i++){
for(int j=0; j < data[i].length; j++){
table.setValueAt(data[i][j], i, j);
}
}
This is the declaration of the method: setValueAt(Object aValue, int row, int column)
If you want to convert an arrayList to an array you can:
ArrayList<String> arrayList;
String[] data = new String[arrayList.size()];
data = arrayList.toArray(data);
Bye Luca
Upvotes: 1
Reputation: 133619
You have two ways:
JTable
by invoking the right constructor (which is JTable(Object[][] rowData, Object[] columnNames)
) and this is quite easy and good but just manages static tablesAbstractTableModel
in which you override the right methods to map the bidimensional collection to the right behaviors.Upvotes: 2