AbstractTableModel: fireTableRowsUpdated(int firstRow, int lastRow) : AbstractTableModel « javax.swing.table « Java by API






AbstractTableModel: fireTableRowsUpdated(int firstRow, int lastRow)

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class MainClass extends JFrame {

  public MainClass() {
    super("Dynamic Data Test");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable jt = new JTable(new MarketDataModel(5));
    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    MainClass mt = new MainClass();
    mt.setVisible(true);
  }
}

class MarketDataModel extends AbstractTableModel implements Runnable {
  Thread runner;
  public MarketDataModel(int initialDelay) {
    Thread runner = new Thread(this);
    runner.start();
  }
  double[] data = new double[]{1,2,3,4,5};
  
  String[] headers = { "Symbol", "Price", "Change", "Last updated" };

  public int getRowCount() {
    return 1;
  }

  public int getColumnCount() {
    return headers.length;
  }

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

  public Object getValueAt(int r, int c) {
    return data[c];
  }


  public void updateStocks() {
    for (int i = 0; i < data.length; i++) {
      data[i] = Math.random();
    }
  }

  public void run() {
    while (true) {
      updateStocks();

      fireTableRowsUpdated(0, data.length - 1);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
      }
    }
  }
}

           
       








Related examples in the same category

1.extends AbstractTableModel (read database table to a JTable)
2.extends AbstractTableModel (Custom class type)
3.extends AbstractTableModel