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]

