File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Display a list in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Display a list in a JTable" Watch "Display a list in a JTable" New topic
Author

Display a list in a JTable

Russell Peters
Ranch Hand

Joined: Jan 25, 2006
Posts: 50
Hey,
An existing part of my program returns an ArrayList and I have to display this in a JTable. Currently, I am converting the list into a vector of vectors and then using "setDataVector()" method of the DefaultTableModel.

But it does not display any data in the JTable. I even tried creating a temporary vector of data and still nothing !
So its not that the program returns a null list.

Also i find this method to be quite cumbersome, having to convert a list into a vector of vectors. Can someone suggest a different way of displaying an ArrayList in a JTable ?

Thanks in advance


SCJP 1.4
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
hi,

Create your own model class that extends from AbstractTableModel and implement the required methods. This is somewhat like following

class MyListTableModel extends AbstractTableModel
{
private ArrayList list;
private final String headers[];

public MyListTableModel(ArrayList list, String[] headers)
{
this.list = list;
this.headers =headers;
}
public int getRowCount()
{
return list.size();
}

public int getColumnCount()
{
return headers.length;
}

public Object getValueAt(int rowIndex, int columnIndex)
{
// do whatever you want to display in table
return list.get(rowIndex);
}

public void setValueAt(Object object,int rowIndex, int columnIndex)
{

}
public String getColumnName(int column) {
return headers[column];
}
}

And set this model to ur Table.

Regards..


~ Anand,
SCJP 1.5
SCWCD 1.5
Russell Peters
Ranch Hand

Joined: Jan 25, 2006
Posts: 50
Hey Anand,

Thanks for your reply.

I did as you suggested and still it does not display any data contained in the arraylist. :-(

Maybe I am doing something wrong. This is what I do after i instantiate the MyListTableModel by passing in the arraylist and the string[] of column names



Is there something else I need to do ?
BTW -- 'list' is the arraylist that my program returns
[ March 20, 2006: Message edited by: Russell Peters ]
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
will u please tell me about ur table means how many columns it has. Accordingly the method getValueAt will vary..
Russell Peters
Ranch Hand

Joined: Jan 25, 2006
Posts: 50
well there are 2 JTables. One has two columns and the other has one column.

SingleColumnTable needs to display a list of codes of String type which my program returns.

DoubleColumnTable displays data from the another list.

I hope i was clear enough
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
If ur table has one column then it should work.
see small program I have done now..


code:
-------------------------------------------------------
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class table extends JFrame
{

public table()
{

setSize(200, 200);
ArrayList<String> list = new ArrayList<String>();
list.add("anand");
list.add("Java");
list.add("Swing");
list.add("developer");
String[] colNames =
{
"List"
};

JTable myTable = new JTable(new MyListTableModel(list, colNames));
getContentPane().add(myTable);
}

public static void main(String[] args)
{

table tab = new table();
tab.setVisible(true);
}

}

class MyListTableModel extends AbstractTableModel
{
private ArrayList list;

private final String headers[];

public MyListTableModel(ArrayList list, String[] headers)
{

this.list = list;
this.headers = headers;
}

public int getRowCount()
{

return list.size();
}

public int getColumnCount()
{
return headers.length;
}

public Object getValueAt(int rowIndex, int columnIndex)
{
return list.get(rowIndex);
}

public void setValueAt(Object object, int rowIndex, int columnIndex)
{

}

public String getColumnName(int column)
{

return headers[column];
}
}
----------------------------------------------------------------
Russell Peters
Ranch Hand

Joined: Jan 25, 2006
Posts: 50
I already have an empty table in my swing application. On clicking a button called 'Register',my program gets called and I get my 'list' populated by my program.
So in a sense, I have to change the existing table to now display the contents of that arraylist.

I am sorry if i didnt mention that clearly.
[ March 20, 2006: Message edited by: Russell Peters ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display a list in a JTable
 
Similar Threads
JTable issues
trouble with JTable
JTable problems
JTable & JDBC
JTable