java 将数据插入 JTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4356505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting data into a JTable?
提问by Dom Minic
I am using the netbeans IDE which comes with a very handy GUI creator tool but have run into trouble.
我正在使用 netbeans IDE,它带有一个非常方便的 GUI 创建器工具,但遇到了麻烦。
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.
我正在创建对数据源的第一个查询并以字符串数组的形式接收该数据的应用程序。我如何将这些数据插入到我使用 GUI 创建器放置在我的窗口中的 jtable 中。
I'm not a complete java newbie so I do know about the code behind that GUI and have done swing programming before.
我不是一个完整的 Java 新手,所以我知道该 GUI 背后的代码,并且以前做过 Swing 编程。
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.
我将如何将第一个数组值插入第一列,然后将第二个数组值插入第二列,我之前没有在 Swing 中使用过 JTable 组件,所以我真的不知道。
Any help would be much appreciated,
任何帮助将非常感激,
回答by Rijul Gupta
You are doing it all wrong mate, in Jtable's defaultTableModel you can add data very easily. for example
您做错了,在 Jtable 的 defaultTableModel 中,您可以非常轻松地添加数据。例如
DefaultTableModel table = (DefaultTableModel) <some JTable>.getModle();
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);
回答by Cameron Skinner
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.
数据不JTable
直接进入;相反,它进入TableModel
. 您可以使用DefaultTableModel
或者您可以创建自己的实现。
It's pretty easy to subclass AbstractTableModel
if DefaultTableModel
doesn't do what you want.
AbstractTableModel
如果DefaultTableModel
没有做你想做的事情,子类化很容易。
回答by Michael Borgwardt
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.
如果您以前做过 Swing 编程,您应该知道 GUI 组件由单独的模型类支持。对于像文本字段这样的简单组件,你可以不用处理那么多,但对于表格,你必须处理TableModel
. 你可以DefaultTableModel
直接使用——它甚至有一个接受二维数组的构造函数。
回答by camickr
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.
好吧,我怀疑您是否会将它们用作列的数据。相反,看起来第一个数组将是 4 列的“标题”值,然后第二个数组将是这 4 列的“数据”值。
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.
阅读 JTable API 并点击有关“如何使用表”的 Swing 教程的链接以获取更多信息和工作示例。