将数据插入 JTable?

发布于 2024-10-06 12:35:05 字数 471 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

是你 2024-10-13 12:35:06

你做错了,伙计,在 Jtable 的 defaultTableModel 中你可以非常轻松地添加数据。
例如

DefaultTableModel table = (DefaultTableModel) myJTable.getModel();
table.addRow{"<column1 value>","<column2 value>"};// maybe even more columns

,从您的两个数组中,即使

String[] tableA_01 = {"Column01","Column02","Column03","Column04"};
String[] tableA_02 = {"Data01","Data02","Data03","Data04"};

数组

String[] row1 = {"Column01","Data01"};
String[] row2 = {"Column02","Data02"};
String[] row3 = {"Column03","Data03"};
String[] row4 = {"Column04","Data04"};

看起来很乏味,但您可以将其放入循环中并使用进行更新

table.addRow(row1);

You are doing it all wrong mate, in Jtable's defaultTableModel you can add data very easily.
for example

DefaultTableModel table = (DefaultTableModel) myJTable.getModel();
table.addRow{"<column1 value>","<column2 value>"};// maybe even more columns

so from your two arrays i.e.

String[] tableA_01 = {"Column01","Column02","Column03","Column04"};
String[] tableA_02 = {"Data01","Data02","Data03","Data04"};

make arrays like

String[] row1 = {"Column01","Data01"};
String[] row2 = {"Column02","Data02"};
String[] row3 = {"Column03","Data03"};
String[] row4 = {"Column04","Data04"};

looks tedious but you can put this in a loop and update by using

table.addRow(row1);
一花一树开 2024-10-13 12:35:06

数据不会直接进入JTable;相反,它会进入 TableModel。您可以使用 DefaultTableModel 也可以创建自己的实现。

如果 DefaultTableModel 不能满足您的要求,那么可以很容易地对 AbstractTableModel 进行子类化。

The data doesn't go into the JTable directly; instead it goes into the TableModel. You can use a DefaultTableModel or you can create your own implementation.

It's pretty easy to subclass AbstractTableModel if DefaultTableModel doesn't do what you want.

甜尕妞 2024-10-13 12:35:06

如果您以前做过 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 use DefaultTableModel directly - it even has a constructor that takes a two-dimensional array.

○闲身 2024-10-13 12:35:06

好吧,我怀疑您是否会将它们用作列的数据。相反,第一个数组将是 4 列的“标题”值,然后第二个数组将是这 4 列的“数据”值。

您的代码将类似于:

DefaultTableModel model = new DefaultTableModel( tablea_01, tableA_02);
JTable table = new JTable( model );

阅读 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:

DefaultTableModel model = new DefaultTableModel( tablea_01, tableA_02);
JTable table = new JTable( model );

Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for more information and working examples.

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