Klausos Klausos
Klausos Klausos

Reputation: 16060

How to refresh JTable after adding new entry

I have a JTable populated from the data structure List<String>. Also I have a button "Create New Field" that is used to create new entries in List. I can add new entry to List<String>, however I don't know how to refresh JTable after adding new entry.

Upvotes: 0

Views: 426

Answers (3)

2sku
2sku

Reputation: 31

1.I have made a new project in that i have selected a new frame and drag a table,a label, a text field and a button from the swing controls to your frame.

2.create a table in back end

3.Download rs2xml jar file from this link Download rs2xml.jar

Add it to the project library

4.Then connect MySQL

5.Then create a update function type the following code...

rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

6.In the buttons action event type the following and call the update function.

Object name=jTextField1.getText();
String sql="insert into details (name) values('"+name+"')";
int  resultset=statement.executeUpdate(sql);

You can follow this tutorial:---

How to refresh jTable after adding new entry

Upvotes: 1

D.R.
D.R.

Reputation: 21224

You have to use tableModel.fireTableDataChanged(). Or if only a very small part (i.e. one cell) has been updated in the table: tableModel.fireTableCellUpdated(x,y).

According to the comments you need to write your own small TableModel.

  • Derive from AbstractTableModel
  • Pass your list to the constructor of your table mdoel
  • Implement required methods accordingly
  • getRowCount returns list.size()
  • getColumnCount returns 1
  • getValueAt returns list.item(row)
  • Use that table model instead of DefaultTableModel
  • If you updated the list, use fireTableDataChanged()

Upvotes: 1

sheu
sheu

Reputation: 304

Use the observer pattern.

http://en.wikipedia.org/wiki/Observer_pattern

Your table model should be an observer to your list. So that when you list data changes the table will be updated.

Upvotes: 0

Related Questions