Updating jtable cells with random numbers

Hello,

I have a made a jtable with column headings and the right amount of rows.

I need to populate the cells with with variables that change without refreshing the whole table.

I have tried to use a variable like so:

String [][]table =new String[][]{new String[]{"1", stat},

new String[]{"2", stat,},

...

But this does not work.

Please advise the simplest way this can be done.

[722 byte] By [John4938a] at [2007-11-26 17:48:29]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import javax.swing.table.AbstractTableModel;

public class Fred908 extends JPanel

{

static class LocalTableModel extends AbstractTableModel

{

private Random random = new Random();

public LocalTableModel()

{

for (int row = 0; row < data.length; row++)

{

for (int col = 0; col < data[row].length; col++)

{

data[row][col] = random.nextGaussian();

}

}

new javax.swing.Timer(100, new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

int row = random.nextInt(getRowCount());

int col = random.nextInt(getColumnCount());

data[row][col] = random.nextGaussian();

fireTableCellUpdated(row, col);

}

}).start();

}

public Object getValueAt(int row, int col)

{

return data[row][col];

}

public int getColumnCount()

{

return data[0].length;

}

public int getRowCount()

{

return data.length;

}

private Double[][] data = new Double[30][5];

}

public Fred908() throws Exception

{

super(new BorderLayout());

add(new JTable(new LocalTableModel()));

}

public static void main(String[] args) throws Exception

{

JFrame frame = new JFrame("Fred908");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new Fred908());

frame.pack();

frame.setVisible(true);

}

}

sabre150a at 2007-7-9 5:00:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

I'm assuming that what you want to have happen is make a change to the

stat variable and the table reflects that change. Unfortunately, there's no

way the table is automatically made aware of changes like that; instead,

you have to tell it about the changes.

The easiest way is once you've changed stat, get the table's model cast

as an AbstractTableModel and invoke fireTableDataChanged().

It might not hurt to review [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables[/url] either.

JayDSa at 2007-7-9 5:00:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

The code that was given was useful.

I am new to GUIs therefore I can only add and organize different objects e.g. Jtable. Jlabel, in a simple way. If I were to add say some radiobuttons and a title to this code, would it be wise to add it in the constructor i.e. public Fred908().

I have managed to do this although I cannot get objects above and below the Jtable.

Any ideas please.

Cheers

John4938a at 2007-7-9 5:00:51 > top of Java-index,Desktop,Core GUI APIs...
# 4
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url]. Mix and match layout managers to get the desired effect.
camickra at 2007-7-9 5:00:51 > top of Java-index,Desktop,Core GUI APIs...