问题描述
我有 JFrame
表单,其中 JTextField
s, JCombobox
等,我能够将这些值接收到变量,现在我想在用户单击Add或类似的东西时将接收的数据添加到新行中的 JTable
。
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.
我使用net-beans创建了 JTable
问题是从那些添加数据的代码是什么变量到表的行。一个基本的例子将不胜感激。我尝试了很多例子,并将代码添加到 JButton
的 ActionListener
,但没有任何事情发生。
我试过的例子是。 和
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?
任何帮助都将不胜感激。
Any Help would be appreciated.
推荐答案
- 设置表列标题
- Set the table column headers
- 在设计视图中高亮显示表格,然后转到右侧的属性窗格。应该是一个标题为属性的标签。确保突出显示该表而不是其周围的滚动窗格,否则下一步将无法工作
- 单击右侧的按钮财产模型。将出现一个对话框。
- 将行设置为0,设置所需的列数及其名称。
- 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
- 右键单击按钮并选择
活动 - >行动 - > actionPerformed
-
你应该看到如下自动生成的代码
- 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) {}
- 右键单击按钮并选择
- Set the table column headers
-
jTable1
将有一个DefaultTableModel
。您可以使用数据向模型添加行 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. }
所以对于每一套通过几个文本字段,组合框和复选框等数据,您可以在每次按下按钮时收集该数据,并将其作为一行添加到模型中。
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.
这篇关于如何从jtextfield和comboboxes接收的值向Jtable添加数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!