• Post Reply Bookmark Topic Watch Topic
  • New Topic

refreshing the JTable  RSS feed

 
sakthivel shan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I want to refresh my JTable...So each and every time of refresh, it should show the updated data in the backend....i am using the below method to do that....i am trying to update my vectordata "Vector"...each time....but, stil it is not getting refreshed....how to do this....

public void refreshTable()
{
vectordata=getData_polleddetailspanel();
jscrollpane.invalidate();
jscrollpane.validate();
jscrollpane.repaint();
jtable.invalidate();
jtable.validate();
jtable.repaint();
}
waiting for ur reply,
Sakthivel S.

COMPLETE CODE:
****************
class PolledDetailsPanel extends JPanel
{
Vector vectorcolnames;
JTable jtable;
JScrollPane jscrollpane;
public PolledDetailsPanel()
{
vectorcolnames = new Vector(7);
vectorcolnames.addElement("File Type");
vectorcolnames.addElement("File Name");
vectorcolnames.addElement("Description");
vectorcolnames.addElement("Status");
vectorcolnames.addElement("Reason");
vectorcolnames.addElement("Created Date");
vectorcolnames.addElement("Modified Date");
mytablemodel2 = new MyTableModel(vectordata,vectorcolnames);
jtable = new JTable(mytablemodel2);
jtable.getTableHeader().setReorderingAllowed(false);
jtable.setRowSelectionAllowed(true);
jtable.setGridColor(Color.green);
jtable.setSelectionMode(0);
jtable.setShowHorizontalLines(true);
jtable.setPreferredScrollableViewportSize(new Dimension(520,150));
JPanel basepanel = new JPanel();
JPanel tablepanel = new JPanel();
JPanel buttonpanel = new JPanel();
basepanel.setLayout(new BoxLayout(basepanel, BoxLayout.Y_AXIS));
JButton delete = new JButton("Delete Data");
JButton save = new JButton("Save Data");
JButton close = new JButton("Refresh");
delete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
save.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
close.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
jscrollpane = new JScrollPane(jtable);
tablepanel.add(jscrollpane,"Center");
buttonpanel.add(delete);
buttonpanel.add(save);
buttonpanel.add(refresh);
basepanel.add(tablepanel);
basepanel.add(buttonpanel);
add(basepanel);
delete.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent aevent)
{
String command=aevent.getActionCommand();
if(command.equals("Delete Data"))
{
int selected = jtable.getSelectedRow();
if (selected >=0)
{
javax.swing.table.DefaultTableModel model1=(javax.swing.table.DefaultTableModel) jtable.getModel();
model1.removeRow(selected);
}
}
}
});
save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent aevent)
{
String command=aevent.getActionCommand();
if(command.equals("Save Data"))
{
if(jtable.isEditing())
{
int row = jtable.getEditingRow();
int col = jtable.getEditingColumn();
jtable.getCellEditor(row,col).stopCellEditing();
}
int entry=0;
Vector basevector = mytablemodel2.getDataVector();
int cap_outer = basevector.size();
for(int index_outer=0;index_outer<cap_outer;index_outer++)
{
Vector innervector = (Vector)basevector.elementAt(index_outer);
int cap_inner = innervector.size();
for(int index_inner=0;index_inner<cap_inner;index_inner++)
{
if((innervector.elementAt(index_inner)==null) || (innervector.elementAt(index_inner).equals("")))
{
entry=1;
JOptionPane.showMessageDialog(jframe,"Please fill all the values","Error Message",JOptionPane.INFORMATION_MESSAGE);
break;
}
}
innervector=null;
if(entry==1)
break;
}
basevector=null;
if(entry==0)
{
saveData_polleddetailspanel();
JOptionPane.showMessageDialog(jframe,"Data Saved Successfully");
}
entry=0;
}
}
});
refresh.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent aevent){
refreshTable();
}});
}
*************************
public Vector getData_polleddetailspanel()
{
Vector vectordata=new Vector();
try
{
ResultSet resultset=stmt.executeQuery("Select * from pollingstatus");
Vector vector;
while(resultset.next())
{
vector = new Vector(7);
vector.addElement(resultset.getString(1));
vector.addElement(resultset.getString(2));
vector.addElement(resultset.getString(3));
vector.addElement(resultset.getString(4));
vector.addElement(resultset.getString(5));
vector.addElement(resultset.getString(6));
vector.addElement(resultset.getString(7));
vectordata.addElement(vector);
}
}
catch(Exception exp)
{exp.printStackTrace();}
System.out.println(vectordata.size());
return(vectordata);
}
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a TableModel that inherits from AbstractTableModel or DefaultTableModel and give it as argument to your JTable. Change the data in the model and the model will notify _automatically_ the JTable which will update itself. no more stressing around with invalidate, revalidata, paint, update, or whatever.
Chantal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!