将数组加载到 Java 表中

发布于 2024-09-06 08:56:36 字数 697 浏览 3 评论 0原文

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

class dataObject{
String name;
String gender;
Byte age;

public dataObject (String name, String gender, Byte age){
   this.name = name;
   .
   .

}

然后我创建一个由此类内容组成的向量:

Vector v = new Vector(99);

v.addElement(new dataObject("Marrie", "Female", 33);
v.addElement(new dataObject("John", "Male", 32);

使用 dataObject 我会收集信息,现在我到底如何在表格中显示它?因为这不起作用:

JTable newTable = new Jtable(v, header) // header is another Vector.

我收到一些错误,导致我看到最后一行。因此,任何帮助,即使是很小的帮助,都会受到赞赏。我知道有几个与此相关的主题,但那些人已经对 JTable + TableModel 的工作原理感到惊讶,我只是勉强明白。

多谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

早乙女 2024-09-13 08:56:36

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

  1. 2D Object 数组
  2. 元素为 VectorVector

,因此您可以这样做:

 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 让生活变得简单,因为您只需要实现三个方法即可开始:

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

前两个很简单,您可以获得 Vector 的行数大小和对列计数的 val 进行硬编码。 getValueAt 是从 DataObject 提取数据的位置。

下面是一个使用匿名类扩展 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,无需担心。

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.

幼儿园老大 2024-09-13 08:56:36

问题是,您使用的构造函数被设计为保存一个包含其他向量的向量。

每一张都有信息。

请参阅此工作示例以更好地理解它:

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

    }
}

它创建:

类似这样的 http://img695 .imageshack.us/img695/2032/capturadepantalla201006r.png

以防万一,您应该看看这个:

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

如果您还没有完成。

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 http://img695.imageshack.us/img695/2032/capturadepantalla201006r.png

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.

各空 2024-09-13 08:56:36

您无法将数据对象加载到 DefaultTableModel 中。您需要创建一个自定义 TableModel 来执行此操作。 Bean Table Model 就是这样一个模型,可以使这个过程对你来说更容易。

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.

依 靠 2024-09-13 08:56:36

我以前从未使用过 JTable,但文档说构造函数采用“向量的向量”作为第一个参数,而不是数据对象的向量。

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.

伴梦长久 2024-09-13 08:56:36

我知道很多人可能会对包含另一个 jar 文件持谨慎态度,但说实话,无论 JTable(或 JList 或 JComboBox)多么简单,我总是使用 GlazedLists 库。坦率地说,它是您使用过的最令人惊奇的库之一。它非常非常灵活。但一个简单的示例是将您的 bean 放入一个名为 EventList 的特殊列表中。然后构建表格格式;通过将格式绑定到数据列表来创建模型,然后设置为表的模型。

假设您有一个 Person 类:

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

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

现在,让您的表显示这些人的列表:

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

我是凭记忆写的,但我认为它或多或少是正确的。使用这个库的好处是可以非常轻松地向表中添加排序和过滤功能。首先让基本表格正常工作,然后开始在 GlazedLists 网站上查找,看看您还可以做什么。还有一些非常好的截屏

PS 我与这个图书馆没有任何关系,我只是觉得它很棒!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文