hi
I am using DefaultTableModel instead of Abstract one to insert data.
This is he main JTable class-
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
public class MainWindow extends javax.swing.JFrame implements TableModelListener {
public MainWindow() {
initComponents();
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
// RVTableModel model = new RVTableModel();
TableModelWrapper wrap = new TableModelWrapper();
RVDataTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
RVDataTable.setModel(wrap.getModel());
RVDataTable.getModel().addTableModelListener(this);
// RVDataTable.
jScrollPane1.setViewportView(RVDataTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 552, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(105, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(25, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
public void tableChanged(TableModelEvent evt){
System.out.println(evt.getFirstRow());
}
// Variables declaration - do not modify
private javax.swing.JTable RVDataTable;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
Following class is used to define DefaultTableModel:
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
public class TableModelWrapper {
public DefaultTableModel getModel(){
Vector dataVec = new Vector();
String[] arr = {"ABS","ASAAS","ASAsA","AsaSA"};
Vector rowVec = new Vector();
for(int i=0;i<4;i++)
rowVec.add(i,arr[i] );
for(int i=0;i<5;i++)
dataVec.add(i, rowVec);
Vector columnNames = new Vector();
columnNames.add("FIRST");
columnNames.add("SECOND");
columnNames.add("THIIRD");
columnNames.add("FOURTH");
//columnNames.add("FIFTH");
DefaultTableModel model = new DefaultTableModel(dataVec,columnNames);
return model;
}
public Object[] getRecord(){
String[] arr = {"hhaHHA","HHHDS","NHHHSS","PQPPPQ"};
return arr;
}
}
and follwing class adds record every 5 seconds using Stock class.
public class Updater {
public static void main(String[] args){
Stock stock =new Stock();
stock.start();
}
}
class Stock extends Thread{
//RVTableModel model = null;
TableModelWrapper wrap = null;
public Stock(){
wrap = new TableModelWrapper();
}
public void run(){
try{
while(true){
wrap.getModel().addRow(wrap.getRecord());
wrap.getModel().fireTableRowsInserted(wrap.getModel().getRowCount() +1,wrap.getModel().getRowCount()+1);
sleep(5000);
}
}
catch(InterruptedException e){
System.out.println(e);
}
}
}
I don't see JTable getting updated.