如何从jtextfield和comboboxes接收的值向Jtable添加数据行 [英] How to add row of data to Jtable from values received from jtextfield and comboboxes

查看:187
本文介绍了如何从jtextfield和comboboxes接收的值向Jtable添加数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JFrame 表单,其中 JTextField s, JCombobox 等,我能够将这些值接收到变量,现在我想在用户单击Add或类似的东西时将接收的数据添加到新行中的 JTable

I have a JFrame Form which has JTextFields, 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 ,但没有任何事情发生。
我试过的例子是。 如何在JTable中添加行?如何使用AbstractTableModel方法向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?

任何帮助都将不胜感激。

Any Help would be appreciated.

推荐答案


Peeskillet蹩脚的工作教程使用Netbeans GUI Builder中的JTable

Peeskillet's lame tutorial for working with JTables in Netbeans GUI Builder




  • 设置表列标题

    • Set the table column headers


      1. 在设计视图中高亮显示表格,然后转到右侧的属性窗格。应该是一个标题为属性的标签。确保突出显示该表而不是其周围的滚动窗格,否则下一步将无法工作

      2. 单击右侧的 ... 按钮财产模型。将出现一个对话框。

      3. 将行设置为0,设置所需的列数及其名称。

      1. 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
      2. Click on the ... button to the right of the property model. A dialog should appear.
      3. 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


      1. 右键单击按钮并选择活动 - >行动 - > actionPerformed

      2. 你应该看到如下自动生成的代码

      1. Right-click on the button and select Events -> Action -> actionPerformed
      2. You should see code like the following auto-generated

      private void jButton1ActionPerformed(java.awt.event.ActionEvent) {}
      



    • jTable1 将有一个 DefaultTableModel 。您可以使用数据向模型添加行

    • The jTable1 will have a DefaultTableModel. You can add rows to the model with your data

      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.

      这篇关于如何从jtextfield和comboboxes接收的值向Jtable添加数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆