tags:

views:

202

answers:

5

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.

A: 

I've never used JTable before, but the documentation says that the constructor takes a "Vector of Vectors" as the first parameter, and not a Vector of dataObjects.

dsmith
+1  A: 

You can't load data objects into the DefaultTableModel. You need to create a custom TableModel to do this. The Bean Table Model is such a model that can make this process easier for you.

camickr
+3  A: 

There are two ways you can create a JTable with a basic, prepared dataset:

  1. a 2D Object array
  2. a Vector whose elements are Vector

so you can do this:

 Object [][] model = {{"Marrie", "Female","33"},{"John","Male","32"}};
 JTable table = new JTable(model);

or you could do this:

 Vector model = new Vector();
 Vector row = new Vector();

 row.add("Marrie");
 row.add("Female");
 row.add("33");
 model.add(row);

 row = new Vector();
 row.add("John");
 row.add("Male");
 row.add("32");
 model.add(row);

 JTable table = new JTable(model);

The next step would be to implement your own TableModel to utilize the DataObject class that you have put together (note that Java classes start with caps). Extending AbstractTableModel makes life easy, as you only need to implement three methods to get started:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

the first two are easy, you can get the size of your Vector for row count and hard-code the val for column count. getValueAt is where you pull the data from your DataObject

Here is an example using an anonymous class, extending AbstractTableModel.

final Vector<DataObject> myDataObjects = new Vector<DataObject>();
myDataObjects.add(...);// add your objects
JTable table = new JTable(new AbstractTableModel() {

    public int getRowCount() {return myDataObjects.size();}
    public int getColumnCount() { return 3; }
    public Object getValueAt(int row, int column){
         switch (column) {
           case 0:
              return myDataObjects.get(row).getName();
           case 1:
              return myDataObjects.get(row).getGender();
           case 2:
              return myDataObjects.get(row).getAge();
           default:
              return "";
         }
    }
});

I have kept the Vector so as to keep it close to your current implementation. You can easily change that to an ArrayList in this example without any worries.

akf
Looking yours and Support's code, i believe it's trivial to have a "data object". The data is just put into the model filling an array and then accessed via the model.
Gabriel A. Zorrilla
I agree, in this example, it is a pretty simple data structure. It is useful in this here just to show how to use a custom object backing a row in a `TableModel`.
akf
+3  A: 

The problem is, the constructor you're using was designed to hold a vector which holds other vectors.

Each one with the information.

See this working sample to understand it better:

import javax.swing.*;
import java.util.Vector;

public class TableDemo {
    public static void main( String [] args ){
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();

        Vector<Object> row = new Vector<Object>();
        row.add( "Marie");
        row.add( "Female");
        row.add( 33);
        data.add(row);

        Vector<Object> otherRow = new Vector<Object>();
        otherRow.add( "John");
        otherRow.add( "Male");
        otherRow.add( 32 );
        data.add(otherRow);

        Vector<String> headers = new Vector<String>();
        headers.add("Name");
        headers.add("Gender");
        headers.add( "Age");


        JTable table = new JTable( data, headers );

        JFrame frame = new JFrame();
        frame.add( new JScrollPane( table ));
        frame.pack();
        frame.setVisible( true ); 

    }
}

Which creates:

something like this

Just in case, you should take a look at this:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

If you haven't done yet.

OscarRyz
A: 

I know a lot of people can be wary of including yet another jar file, but to be honest, no matter how simple the JTable (or JList or JComboBox), I always utilise the GlazedLists library. Quite frankly it's one of the most amazing libs you'll ever use. It's very, very flexible. But a simple example consists of putting your beans into a special list called EventList. Then construct a table format; create the model by binding the format to the data list, and then set as the table's model.

Assume you have a Person class:

public class Person {
  private String firstName;
  private String surname;
  private int age;

  ... standard constructors, getters and setters...
}

Now, to make your table display a list of these people:

EventList<Person> peopleEventList = new BasicEventList<Person>();
peopleEventList.add(... create some objects and add it the usual way ...);
...

String[] columnProperties = { "firstName", "surname", "age" };
String[] columnLabels = { "First name", "Surname", "Age" };
TableFormat personTableFormat = GlazedLists.tableFormat(columnProperties, columnLabels);
EventTableModel personTableModel = new EventTableModel(peopleEventList, personTableFormat);
myJTable.setModel(personTableModel);

I'm writing this from memory, but I think it's more or less correct. The great thing with using this library is it's seriously easy to add sorting and filtering to the table. Get the basic table working first, then start looking on the GlazedLists site to see what else you can do. There are some really good screencasts too.

PS I'm in no way affiliated with this library, I simply think it rocks!

arooaroo