Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

jtable - Load an array to a Java table

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...