This is driving me crazy. I read the Sun’s tutorial regarding the creation of a basic table with a default data model, but cant figure out a simple example about how to load an array of data-objects like:
class dataObject{
String name;
String gender;
Byte age;
public dataObject (String name, String gender, Byte age){
this.name = name;
.
.
}
Then i create, for example, a vector of this stuff:
Vector v = new Vector(99);
v.addElement(new dataObject("Marrie", "Female", 33);
v.addElement(new dataObject("John", "Male", 32);
With dataObject i’d gather the info, now how the heck i show it in a table? Because this is not working:
JTable newTable = new Jtable(v, header) // header is another Vector.
I’m getting some errors that lead me to this last line. So, any help, even little, is apreciated. I know there are several threads about this, but those people already have a gasp about how JTable + TableModel works, I just barely get it.
Thanks a lot.
There are two ways you can create a JTable with a basic, prepared dataset:
ObjectarrayVectorwhose elements areVectorso you can do this:
or you could do this:
The next step would be to implement your own
TableModelto utilize theDataObjectclass that you have put together (note that Java classes start with caps). ExtendingAbstractTableModelmakes life easy, as you only need to implement three methods to get started:the first two are easy, you can get the size of your
Vectorfor row count and hard-code the val for column count.getValueAtis where you pull the data from yourDataObjectHere is an example using an anonymous class, extending
AbstractTableModel.I have kept the
Vectorso as to keep it close to your current implementation. You can easily change that to an ArrayList in this example without any worries.