How to add row of data to Jtable from values received from jtextfield and comboboxes
Asked Answered
O

3

16

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.

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.

Otology answered 15/1, 2014 at 10:53 Comment(3)
During the creation of jtable, you would use a table model. Call the addRow() method of table model to add the new row of data.Consol
Agreed sir I know that this is related to Netbeans but i want to basic idea how i will add the data to row received. A basic example or already existing example would suffice that fulfills my needOtology
@user3197695 but i want to basic idea how i will add the data to row received - everything is described in official Oracle tutorial - How to use Tables, A basic example or already existing example would suffice that fulfills my need - there are working code examplesDeibel
P
27

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

  • Set the table column headers
    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. 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) {}
      
  • 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.

Princess answered 15/1, 2014 at 11:7 Comment(3)
peeskillet and @eatSleepCode Thank you so much you people saved me. Really thanks alot. I achieved what i wanted.Otology
I was helped by this postSchiffman
Don't be so modest peeskillet, this is a great tutorial!Hebdomadal
M
10

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.

Miletus answered 15/1, 2014 at 11:2 Comment(1)
Got it buddy have accomplished what i wantedOtology
A
7
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.

Arsenious answered 17/3, 2018 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.