将数组加载到 Java 表中
这让我发疯。我阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过两种方法使用准备好的基本数据集创建 JTable:
Object
数组Vector
的Vector
,因此您可以这样做:
或者您可以这样做:
下一步是实现您自己的
TableModel
以利用您组合在一起的DataObject
类(请注意,Java 类以上限)。扩展AbstractTableModel
让生活变得简单,因为您只需要实现三个方法即可开始:前两个很简单,您可以获得
Vector
的行数大小和对列计数的 val 进行硬编码。getValueAt
是从DataObject
提取数据的位置。下面是一个使用匿名类扩展
AbstractTableModel
的示例。我保留了
Vector
以便使其接近您当前的实现。在本例中,您可以轻松地将其更改为 ArrayList,无需担心。There are two ways you can create a JTable with a basic, prepared dataset:
Object
arrayVector
whose elements areVector
so you can do this:
or you could do this:
The next step would be to implement your own
TableModel
to utilize theDataObject
class that you have put together (note that Java classes start with caps). ExtendingAbstractTableModel
makes 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
Vector
for row count and hard-code the val for column count.getValueAt
is where you pull the data from yourDataObject
Here is an example using an anonymous class, extending
AbstractTableModel
.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.问题是,您使用的构造函数被设计为保存一个包含其他向量的向量。
每一张都有信息。
请参阅此工作示例以更好地理解它:
它创建:
类似这样的 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:
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.
您无法将数据对象加载到 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.
我以前从未使用过 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.
我知道很多人可能会对包含另一个 jar 文件持谨慎态度,但说实话,无论 JTable(或 JList 或 JComboBox)多么简单,我总是使用 GlazedLists 库。坦率地说,它是您使用过的最令人惊奇的库之一。它非常非常灵活。但一个简单的示例是将您的 bean 放入一个名为 EventList 的特殊列表中。然后构建表格格式;通过将格式绑定到数据列表来创建模型,然后设置为表的模型。
假设您有一个 Person 类:
现在,让您的表显示这些人的列表:
我是凭记忆写的,但我认为它或多或少是正确的。使用这个库的好处是可以非常轻松地向表中添加排序和过滤功能。首先让基本表格正常工作,然后开始在 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:
Now, to make your table display a list of these people:
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!