0

I want to add rows in JTable, but it didn't work well. Could someone help me? Table is displaying normal but not dynamically

//displays all data in Jtable
void refresh()
{
    Vector<Vector<String>> data = new Vector<>();

        ResultSet rs = st.executeQuery("SELECT * FROM tblInfo");

        while(rs.next())
        {           
        Vector<String> d = new Vector<>();
        d.add(rs.getString("ID"));  
        d.add(rs.getString("Name"));
        d.add(rs.getString("User"));
        d.add(rs.getString("Pass"));
        data.add(d);
        }


        Vector<String> header = new Vector<>();
        header.add("ID");
        header.add("Name");
        header.add("Username");
        header.add("Password");

        model = new DefaultTableModel(data, header);
        table = new JTable(model);
        st.close();
        rs.close();

        table.setBackground(Color.LIGHT_GRAY);
        table.setForeground(Color.white);
        scroll = new JScrollPane(table);
        getContentPane().add(scroll);

    st.close();
    rs.close();
}

//adding data to database
void addDoctor()
    {


        st.executeUpdate("INSERT INTO tblInfo(Name) VALUES ('Name')");  

    st.close();
}




public void actionPerformed(ActionEvent e){ 
    Object source = e.getSource();


    else if(btnAdd == source)
    {

        addDoctor();
        refresh();      
    }

Thanks for any response. :) I have edited this code before i've posted.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • What does this mean "Table is displaying normal but not dynamically"? Do you see no data in the Table? – keuleJ Mar 01 '13 at 09:42
  • I can see the data in table. The problem is, after adding data, it doesn't reflecting in JTable, unless I will rerun the program. – Jedrico Hermoso Mar 01 '13 at 10:00
  • Please edit your question to include an [sscce](http://sscce.org/) that shows the problem you describe. – trashgod Mar 01 '13 at 10:16
  • To refresh an existing table, just recreate the model and then use `table.setModel(...);`. – camickr Mar 01 '13 at 16:26

2 Answers2

1

1) Don't create any Objects inside try - catch - finally block; for Swing GUI, prepare these Objects before, better as local variables.

2) You created a new

model = new DefaultTableModel(data, header);
table = new JTable(model);

and those Object maybe never added to the already visible GUI. Swing GUI doesn't care somehow, and the container doesn't know that you changed (reset, reinitialize) the underlaying model and with JTable. You have to notify Swing GUI for changes, but this isn't the proper of way.

3) Don't to recreate this Object on runtime, reuse Objects that already exist, create JTable and DefaultTableModel only one time.

4) Reset DefaultTableModel by using model.setRowCount(0); and then to add a new rows from JDBC

5) Don't to reinvent the wheel, search for ResultSetTableModel or TableFromDatabase.

6) Move code lines st.close(); & rs.close(); to the finally block.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I don't know what's the problem. My JTable is not refreshing. What do I need to do? – Jedrico Hermoso Mar 01 '13 at 10:08
  • "Note that passing in a `null` value for `dataVector` results in unspecified behavior, and possibly an exception." See also this [answer](http://stackoverflow.com/a/15057052/230513). – trashgod Mar 01 '13 at 10:12
  • 1
    Glad to help; that was my learning item from that day; +1 for other points; sorry for heavy editorial hand. – trashgod Mar 01 '13 at 11:11
0

Use DefaultTableModel.setDataVector() to add a new Vector with the new Data to the existing TableModel/JTable. Or use the insertRow/removeRow methods. Or implement your own AbstractTableModel.

keuleJ
  • 3,418
  • 4
  • 30
  • 51
  • how to use the insertRow method? Can you teach me please? Thanks. – Jedrico Hermoso Mar 01 '13 at 10:27
  • With the insertRow(int row, Vector rowData) method you just insert a row in the table somewhere. With the row param you say at which point you want to insert the rowData. – keuleJ Mar 01 '13 at 11:35