Jtabel refresh

[nobr]Hi.

Before i go any further, I HAVE searched the forums / google, its where i found part of my solution.

I'm having a problem getting the table to refresh. I created a Actionlister which is on a timer, to refresh every second. My table however is not being updated, as data is being added / removed from the website it is reading (populating) the data from.

Can anyone shed any light on this?

sampleJTable =new JTable(cells, columnNames);

JScrollPane tablePane =new JScrollPane(sampleJTable);

tablePane.setPreferredSize(new Dimension(420, 355));

int delay = 1000;//milliseconds

ActionListener taskPerformer =new ActionListener(){

publicvoid actionPerformed(ActionEvent evt){

sampleJTable.setModel(sampleJTable.getModel());

}

};

new Timer(delay, taskPerformer).start();

PS. I'll post the entire code for the class. It is a stand alone so you can compile and then execute it so you can see the problem yourself.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

import javax.swing.table.DefaultTableModel;

publicclass GetFarmsextends JPanel{

publicstaticvoid main(String[] args)throws Exception{

JButton RefreshFarms =new JButton("Refresh Farm List");

JFrame frame =new JFrame("Covenant Farm List");

frame.add(new GetFarms());

frame.setSize(450,400);

frame.setVisible(true);

}

publicstatic String newline = System.getProperty("line.separator");

publicint GetNumberFarms()throws MalformedURLException, IOException{

URL theUrl =new URL("http://www.allydm.co.uk/covenant/total_farms.php");

BufferedReader in =new BufferedReader(

new InputStreamReader(

theUrl.openStream()));

String NumberFarms = in.readLine();

int NumberFarmsRaw = Integer.parseInt(NumberFarms);

return NumberFarmsRaw;

}

privatefinalint COLUMNS = 4;

privateint ROWS = GetNumberFarms();

private JTable sampleJTable;

private String[][] cells =new String[ROWS][COLUMNS];

public GetFarms()throws Exception{

URL theUrl =new URL("http://www.allydm.co.uk/Covenant/farm_list.php");

BufferedReader in =new BufferedReader(

new InputStreamReader(

theUrl.openStream()));

String inputLine = in.readLine();

String[] lines = inputLine.split("<br>");

for (int i = 0; i < ROWS; i++){

String[] stuff = lines[i].split(" ");

for (int j = 0; j < COLUMNS; j++){

cells[i][j] = stuff[j];

}

}

in.close();

final String[]columnNames ={"Username","DA","Sentry","Last Update"};

sampleJTable =new JTable(cells, columnNames);

JScrollPane tablePane =new JScrollPane(sampleJTable);

tablePane.setPreferredSize(new Dimension(420, 355));

int delay = 1000;//milliseconds

ActionListener taskPerformer =new ActionListener(){

publicvoid actionPerformed(ActionEvent evt){

sampleJTable.setModel(sampleJTable.getModel());

}

};

new Timer(delay, taskPerformer).start();

add(tablePane, BorderLayout.CENTER);

}

}

[/nobr]

[6207 byte] By [allydma] at [2007-11-26 22:01:08]
# 1

If you want help in the future then listen to the advice given to you in the past.

I'm not going to read code when you can't learn the proper naming conventions. These a standard naming conventions the every Java programmer uses. They are followed by the JDK and users of the forum. Its confusing trying to read your code. If you can't make a little effort to make it easy for us to read your code, then I'm not going to bother to help.

> sampleJTable.setModel(sampleJTable.getModel());

What do you expect to happen? Setting the model to itself, does not change the data in the model. You need to create a new model every time and add the new data to the model.

camickra at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sorry, i hadn't gone back through and renamed all my methods to the mixed case conventions. I have just finished doing so, and will try to remember to do so in future.

How can i call a new model, whilst keeping it the same? I don't want the structure of the table to change, just the data inside it.

Hope you understand what i mean.

allydma at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 3
> I don't want the structure of the table to change, just the data inside itAfter you create the table the first time you can use:table.setAutoCreateColumnsFromModel( false );
camickra at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi.

Thankyou for the above, it does indeed work on the first refresh, however after that it dosn't seem to (as suggested in your post).

I am currently trying to get:

sampleJTable.setModel(cells, columnNames);

To work, however i get the following error:

Compiling 1 source file to C:\Documents and Settings\Administrator\Desktop\Covenant\Covenant\build\classes

C:\Documents and Settings\Administrator\Desktop\Covenant\Covenant\src\covenant\GetFarms.java:81: setModel(javax.swing.table.TableModel) in javax.swing.JTable cannot be applied to (java.lang.String[][],java.lang.String[])

sampleJTable.setModel(cells, columnNames);

1 error

BUILD FAILED (total time: 4 seconds)

I think its because my tables "cells" is built differently, and so do you have any suggestions as to a way of refreshing my table data repeatidly?

Thankyou for your help with this.

allydma at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 5
This function does not exist in JTable (setModel with parameters cells and columnNames).setModel should be called with TableModel.Use this instead:sampleJTable.setModel(new DefaultTableModel(cells, columnNames));
Rodney_McKaya at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thankyou for the reply.

I have ammended my code, however it still dosn't want to work for me.

I have a feeling, that when i'm calling cells in my action listener

sampleJTable.setModel(new DefaultTableModel(cells, columnNames));

Its just returning the value that it got when initiated, and isn't actually getting a new value for cells. (So the same data would be displayed).

Can anyone suggest a work around / solution to this?

Thankyou for the help :D

allydma at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 7
You need of course to run the piece of code that reads from the url and build the cells again, and put the new cells inside a DefaultTableModel and use setModel to apply it to the table.
Rodney_McKaya at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 8
Sorry for being slow with this.I'm not sure how i can get my table building code to execute again. Unless i've missed something i can't get it to. Spent the last hour playing around with the code and it just dosn't seem to be happening.
allydma at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...
# 9

> I'm not sure how i can get my table building code to execute again.

You create a method called "createTableModel". Then you take to the code from the constructor that reads your URL and build the TableModel and you move that code into the method. Now you can invoke this method from the constructor and from your Timer.

camickra at 2007-7-10 10:36:36 > top of Java-index,Desktop,Core GUI APIs...