将数组加载到Java表 [英] Load an array to a Java table

查看:154
本文介绍了将数组加载到Java表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯了。我阅读了Sun关于使用默认数据模型创建基本表的教程,但是无法找到一个关于如何加载数据对象数组的简单示例:

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);

使用dataObject我会收集信息,现在我怎么在表中显示它?因为这是不工作:

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.

我遇到了一些错误,导致我走到了最后一行。所以,任何帮助,甚至很少,都是相关的。我知道关于这一点有几个主题,但那些人已经对JTable + TableModel的工作方式感到厌烦,我几乎得不到它。

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.

非常感谢。

推荐答案

有两种方法可以使用基本的准备数据集创建JTable:

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


  1. 一个2D 对象数组

  2. 一个 Vector 其元素是 Vector

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

所以你可以这样做:

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

或者你可以这样做:

 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);

下一步是实现你自己的 TableModel 使用你放在一起的 DataObject 类(注意Java类以大写字母开头)。扩展 AbstractTableModel 让生活更轻松,因为您只需要实现三种方法即可开始:

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);

前两个很容易,你可以得到 Vector <的大小/ code>用于行计数并硬编码列数的val。 getValueAt 是从 DataObject

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

这是一个使用匿名类的示例,扩展 AbstractTableModel

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 "";
         }
    }
});

我保留了 Vector 以便使它接近您当前的实施。您可以轻松地将此更改为此示例中的ArrayList,而无需担心。

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.

这篇关于将数组加载到Java表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆