Java 如何从从 jtextfield 和组合框接收到的值向 Jtable 添加一行数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21135452/
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
How to add row of data to Jtable from values received from jtextfield and comboboxes
提问by Nawaz
I have a JFrame
Form which has JTextField
s, JCombobox
etc. and I am able to receive those values to variables and now I want to add the received data to JTable
in new row when user clicks Add or something like that.
我有一个JFrame
包含JTextField
sJCombobox
等的表单,我能够将这些值接收到变量中,现在我想JTable
在用户单击“添加”或类似内容时将接收到的数据添加到新行中。
I have created JTable
using net-beans the problem is what would be the code to add data from those variable to the rows of table. A basic example would be appreciated. I have tried numerous example and have added the code to ActionListener
of the JButton
but nothing Happens.
The Examples I tried are. How to add row in JTable?and How to add rows to JTable with AbstractTableModel method?
我JTable
使用 net-beans创建了问题是将数据从这些变量添加到表行的代码是什么。一个基本的例子将不胜感激。我已经尝试了许多示例,并已将代码添加到ActionListener
的JButton
但没有发生任何事情。我试过的例子是。如何在JTable中添加行?以及如何使用 AbstractTableModel 方法向 JTable 添加行?
Any Help would be appreciated.
任何帮助,将不胜感激。
回答by eatSleepCode
you can use this code as template please customize it as per your requirement.
您可以将此代码用作模板,请根据您的要求对其进行自定义。
DefaultTableModel model = new DefaultTableModel();
List<String> list = new ArrayList<String>();
list.add(textField.getText());
list.add(comboBox.getSelectedItem());
model.addRow(list.toArray());
table.setModel(model);
here DefaultTableModel
is used to add rows in JTable
,
you can get more info here.
hereDefaultTableModel
用于在 中添加行JTable
,您可以在此处获取更多信息。
回答by Paul Samsotha
Peeskillet's lame tutorial for working with JTables in Netbeans GUI Builder
Peeskillet 在 Netbeans GUI Builder 中使用 JTables 的蹩脚教程
- Set the table column headers
- Highglight the table in the design view then go to properties pane on the very right. Should be a tab that says "Properties". Make sure to highlight the table and not the scroll pane surrounding it, or the next step wont work
- Click on the ...button to the right of the property model. A dialog should appear.
- Set rows to 0, set the number of columns you want, and their names.
Add a button to the frame somwhere,. This button will be clicked when the user is ready to submit a row
- Right-click on the button and select
Events -> Action -> actionPerformed
You should see code like the following auto-generated
private void jButton1ActionPerformed(java.awt.event.ActionEvent) {}
- Right-click on the button and select
The
jTable1
will have aDefaultTableModel
. You can add rows to the model with your dataprivate void jButton1ActionPerformed(java.awt.event.ActionEvent) { String data1 = something1.getSomething(); String data2 = something2.getSomething(); String data3 = something3.getSomething(); String data4 = something4.getSomething(); Object[] row = { data1, data2, data3, data4 }; DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); model.addRow(row); // clear the entries. }
- 设置表格列标题
- 在设计视图中突出显示表格,然后转到最右侧的属性窗格。应该是一个标签,上面写着“属性”。确保突出显示表格而不是围绕它的滚动窗格,否则下一步将不起作用
- 单击...属性模型右侧的按钮。应该会出现一个对话框。
- 将行设置为 0,设置所需的列数及其名称。
在框架某处添加一个按钮。当用户准备好提交一行时,将单击此按钮
- 右键单击按钮并选择
Events -> Action -> actionPerformed
您应该会看到类似以下自动生成的代码
private void jButton1ActionPerformed(java.awt.event.ActionEvent) {}
- 右键单击按钮并选择
该
jTable1
会有一个DefaultTableModel
。您可以使用数据向模型添加行private void jButton1ActionPerformed(java.awt.event.ActionEvent) { String data1 = something1.getSomething(); String data2 = something2.getSomething(); String data3 = something3.getSomething(); String data4 = something4.getSomething(); Object[] row = { data1, data2, data3, data4 }; DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); model.addRow(row); // clear the entries. }
So for every set of data like from a couple text fields, a combo box, and a check box, you can gather that data each time the button is pressed and add it as a row to the model.
因此,对于来自几个文本字段、组合框和复选框的每一组数据,您可以在每次按下按钮时收集这些数据并将其作为一行添加到模型中。
回答by Udeesha Induwara
String[] tblHead={"Item Name","Price","Qty","Discount"};
DefaultTableModel dtm=new DefaultTableModel(tblHead,0);
JTable tbl=new JTable(dtm);
String[] item={"A","B","C","D"};
dtm.addRow(item);
Here;this is the solution.
在这里;这是解决方案。