Java – how to add a row of data to JTable from the values received in jtextfield and combobox
I have a JFrame form with jtextfields, JCombo@R_759_2419 @Wait, I can receive these values to variables. Now I want to add the received data to the new line JTable when the user clicks add or similar
I have created JTable using net beans. The question is what rows to add data from those variables to the table A basic example would be appreciated I've tried many examples and added code to JButton's actionlistener, but nothing happened The example I've tried is How to add row in JTable? And how to add rows to JTable with abstracttablemodel method?
Any help would be appreciated
Solution
>Set table column headings
>Highlight the table in design view and go to the properties pane on the right It should be a label that says "attribute" Make sure to highlight the table instead of the scrolling pane around it, otherwise the next step will not work properly > click the... Button to the right of the attribute model A dialog box will appear. > Set the row to 0 and set the required number of columns and their names
>Add a button to the frame somwhere This button will be clicked when the user is ready to submit the bank
>Right click the button and select event – > action – > as actionperformed > you should see automatically generated code like the following
private void jButton1ActionPerformed(java.awt.event.ActionEvent) {}
>Jtable1 will have a defaulttablemodel You can use data to add rows to the model
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. }
Therefore, for each set of data, such as several text fields, combo boxes and check boxes, you can collect the data every time you press the button and add it to the model as a row