File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to refresh the table ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to refresh the table ?" Watch "How to refresh the table ?" New topic
Author

How to refresh the table ?

vijay Panner
Ranch Hand

Joined: Sep 29, 2010
Posts: 30
Hi

I am developing an application using swing. Here get one input from text field,and based on the input retrieve the data from database then show it on table whenever click search button.so how to refresh the table? please give any example
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

AbstractTableModel#fireTableDataChanged


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Do the SQL queary and create a new TableModel from the result set of the query. Then you update the table by using:

vijay Panner
Ranch Hand

Joined: Sep 29, 2010
Posts: 30
Hi
Data cannot update in the table. when we click button once then data will show in the table, again we click the DefaultTablemodel can update but the data cannot update in the table. please help me .


the code is below


class MyTableModel extends AbstractTableModel {

/**
*
*/
private static final long serialVersionUID = 3557014627615565384L;
//Declare the String
final String[] column_Names = { "ID", "TOTAL SESSION", "AVAILABLE" };
private ArrayList<Object> data = new ArrayList<Object>();
private String uname = "", pword = "", url = "", schema = "";
private Propertyread Propertyfile;
private String Query;
public TableModel abtmodel = null;
Connection conn = null;
Statement stmt = null;

MyTableModel(ResultSet rs)throws SQLException {

// Call the perpertyread class's method is findPropertyfile()

while (rs.next()) {
data.add(rs.getInt("memid"));
data.add(rs.getInt("totalsession"));
data.add(rs.getInt("available"));
}
fireTableDataChanged();

Object[][] Objdata={{data.toArray()}};
abtmodel = new DefaultTableModel(Objdata, column_Names);

System.out.println("abstract 1 "+data);
}

public int getColumnCount() {
// TODO Auto-generated method stub
return column_Names.length;
}

public int getRowCount() {
// TODO Auto-generated method stub
return 1;
}

public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data.get(columnIndex);


}

public String getColumnName(int c) {
return column_Names[c];
}

public boolean isCellEditable(int row, int col) {
return false;
}

public void setValueAt(Object value, int row, int col) {
data.set(row, value);
this.fireTableDataChanged();
System.out.println("New data Updated");
}

public void addRow(ArrayList row){
data.add(row);
this.fireTableDataChanged();
}

public void delRow(int row) {
data.remove(row);
this.fireTableDataChanged();
System.out.println("delete");
}

public void addResultSet(ResultSet rs)throws SQLException{
// data.clear();
while (rs.next()) {
data.add(rs.getInt("memid"));
data.add(rs.getInt("totalsession"));
data.add(rs.getInt("available"));
}
fireTableDataChanged();

Object[][] Objdata={{data.toArray()}};
abtmodel = new DefaultTableModel(Objdata, column_Names);

System.out.println("abstract 2 "+data);
this.fireTableDataChanged();
}


}
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
1) Use the "Code" tags when posting code.

2) Your code makes no sense. You are creating a DefaultTableModel and attempt to use your own custom TableModel. Get rid of the custom TableModel and just use a DefaultTableModel

3) I gave you a one line solution earlier and Ii don't see where you use the suggestion anywhere.
vijay Panner
Ranch Hand

Joined: Sep 29, 2010
Posts: 30
Hi


Thanks Rob Camick . You gave one advice for how to update the table.but i donot know how to implement that. please give any example.

Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Create a simple GUI with a Jtable and some hard coded data. Then add an "Update" button. when the update button is pressed you hardcode data and refresh your TableModel and update the JTable with the new data. Once you understand how to do this with hardcoded data you can change your "update" routine to read the data from the database.

If you have problems then post your SSCCE that shows what you have tried.
vijay Panner
Ranch Hand

Joined: Sep 29, 2010
Posts: 30
Thanks for your help.
 
Consider Paul's rocket mass heater.
 
subject: How to refresh the table ?