This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to refresh the table ?

 
vijay Panner
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AbstractTableModel#fireTableDataChanged
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic