将数据插入 JTable?
我正在使用 netbeans IDE,它附带了一个非常方便的 GUI 创建工具,但遇到了麻烦。
我创建的应用程序首先查询数据源并以字符串数组的形式接收返回的数据。我如何将此数据插入到我使用 GUI 创建器放入窗口的 jtable 中。
我不是一个完整的 java 新手,所以我确实了解 GUI 背后的代码,并且之前已经完成过 swing 编程。
例如,假设我有两个字符串数组:
String[] tableA_01 = {"Column01","Column02","Column03","Column04"};
String[] tableA_02 = {"Data01","Data02","Data03","Data04"};
如何将第一个数组值插入第一列,然后将第二个数组值插入第二列,我之前没有在 swing 中使用过 JTable 组件,所以我不这样做真的知道。
任何帮助将不胜感激,
I am using the netbeans IDE which comes with a very handy GUI creator tool but have run into trouble.
The application that I am creating first queries to a data source and receives that data back in the form of an array of strings. How would I insert this data into the jtable that I have placed into my window using the GUI creator.
I'm not a complete java newbie so I do know about the code behind that GUI and have done swing programming before.
For example, let's say I have two arrays of strings:
String[] tableA_01 = {"Column01","Column02","Column03","Column04"};
String[] tableA_02 = {"Data01","Data02","Data03","Data04"};
How would I insert the first arrays values into the first column and then the second arrays values into a second column, I have not used the JTable component in swing before so I don't really know.
Any help would be much appreciated,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你做错了,伙计,在 Jtable 的 defaultTableModel 中你可以非常轻松地添加数据。
例如
,从您的两个数组中,即使
数组
看起来很乏味,但您可以将其放入循环中并使用进行更新
You are doing it all wrong mate, in Jtable's defaultTableModel you can add data very easily.
for example
so from your two arrays i.e.
make arrays like
looks tedious but you can put this in a loop and update by using
数据不会直接进入
JTable
;相反,它会进入TableModel
。您可以使用DefaultTableModel
也可以创建自己的实现。如果
DefaultTableModel
不能满足您的要求,那么可以很容易地对AbstractTableModel
进行子类化。The data doesn't go into the
JTable
directly; instead it goes into theTableModel
. You can use aDefaultTableModel
or you can create your own implementation.It's pretty easy to subclass
AbstractTableModel
ifDefaultTableModel
doesn't do what you want.如果您以前做过 Swing 编程,您应该知道 GUI 组件由单独的模型类支持。对于像文本字段这样的简单组件,您无需处理太多内容就可以完成,但对于表格,您必须处理
TableModel
。您可以使用DefaultTableModel
直接 - 它甚至有一个采用二维数组的构造函数。If you've done Swing programming before, you should know that GUI components are backed by separate model classes. For simple components like text fields, you can get by without dealing with those much, but for a table, you have to deal with the
TableModel
. You can useDefaultTableModel
directly - it even has a constructor that takes a two-dimensional array.好吧,我怀疑您是否会将它们用作列的数据。相反,第一个数组将是 4 列的“标题”值,然后第二个数组将是这 4 列的“数据”值。
您的代码将类似于:
阅读 JTable API 并点击有关“如何使用表”的 Swing 教程的链接以获取更多信息和工作示例。
Well I doubt you would use them as data for columns. Instead it looks like the first array will be "header" values for 4 columns and then the second array will be "data" values for those 4 columns.
Your code would be something like:
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for more information and working examples.