JTable is not getting updated Please Help it is urgrent
Hi All,
I am stucked in a problem. i have three classes
Below is the class which extends AbstractTableModel
import javax.swing.table.*;
import java.util.ArrayList;
import java.util.HashMap;
public class ResultTable extends AbstractTableModel
{
private String[] columnNames = {"URL","STATUS"};
protected static int NUM_COLUMNS = 2;
protected static int START_NUM_ROWS = 0;
protected int nextEmptyRow = 0;
protected int numRows = 0;
private ArrayList urls;
private HashMap urlsToStatus;
public ResultTable()
{
//initializing
urls = new ArrayList();
urlsToStatus = new HashMap();
}
public String getColumnName(int column)
{
return columnNames[column];
}
public synchronized int getColumnCount()
{
return NUM_COLUMNS;
}
public synchronized int getRowCount()
{
if (numRows < START_NUM_ROWS)
{
return START_NUM_ROWS;
}
else
{
return numRows;
}
}
public synchronized Object getValueAt(int row, int column)
{
try
{
System.out.println("Getting row "+row+" by column "+column );
switch (column)
{
case 0:
return urls.get(row);
case 1:
return urlsToStatus.get(urls.get(row));
}
}
catch (Exception e)
{
}
return "N/A";
}//getValueAt
public synchronized void updateURLStatus(String url, String status)
{
boolean addedRow = !urls.contains(url) ;
urlsToStatus.put(url, status);
if (addedRow)
{
numRows++;
urls.add(url);
this.fireTableRowsInserted(urls.size(), urls.size());
System.out.println("init");
}
else
{
int row = urls.indexOf(url);
System.out.println("Updated row "+row);
this.fireTableRowsUpdated(row, row);
}
}
}
Below is the class which pings the url after specific interval of time
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
public class WebMonitorEngine extends Thread
{
public HashMap urlsToStatus = new HashMap();
WebMonitorGui ob = new WebMonitorGui(true);
public WebMonitorEngine(boolean distributed)
{
//get the inbound stage and register has handler
}
public void manipulateUrl(String urlLink)
{
// the event is a string like "java.sun.com"
synchronized(urlsToStatus)
{
String urlStr = "";
urlStr = urlLink;
if(!urlStr.startsWith("HTTP://")&& !urlStr.startsWith("http://"))
{
urlStr = "http://"+urlStr;
}
// return a temporary response
urlsToStatus.put(urlStr, "Checking");
ob.handle(urlsToStatus);
}
}
public void run()
{
while(true)
{
try
{
sleep(1500);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
HashMap result = new HashMap();
// check URLs
synchronized(urlsToStatus)
{
Iterator urlsToCheck =urlsToStatus.keySet().iterator();
while(urlsToCheck.hasNext())
{
String urlStr =(String) urlsToCheck.next();
String status = "";
try
{
URL url = new URL(urlStr);
url.openStream().read();
status= "OK :)";
}
catch (Exception e)
{
status = "Not responding";
}
result.put(urlStr, status);
urlsToStatus.put(urlStr, status);
//System.out.println(urlStr + " : " + status);
}// while
if(!result.isEmpty())
{
ob.handle(urlsToStatus);
}
}// while
}// run
}
}
Below is the class which is the main and is creating GUI
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class WebMonitorGui implements ActionListener
{
JTextField urlInput;
ResultTable model = new ResultTable();
JFrame frame;
static WebMonitorEngine engine = new WebMonitorEngine(true);
public WebMonitorGui(boolean distributed)
{
}
public void startGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
frame = new JFrame("WebSiteMonitor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//model.updateURLStatus("test","test");
//model.updateURLStatus("new","new");
JTable result = new JTable(model);
result.setPreferredScrollableViewportSize(new Dimension(250, 50));
JScrollPane scrollPane = new JScrollPane(result);
JPanel imputPan = new JPanel(new GridLayout(0,2));
imputPan.setSize(100, 10);
JButton button = new JButton("Add URL to monitor");
button.addActionListener(this);
button.setMaximumSize(new Dimension(50,25));
urlInput = new JTextField(20);
urlInput.addActionListener(this);
urlInput.setMaximumSize(new Dimension(50,25));
imputPan.add(urlInput);
imputPan.add(button);
JPanel panel = new JPanel(new BorderLayout());
panel.add(imputPan,BorderLayout.NORTH);
panel.add(scrollPane,BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createEmptyBorder());
panel.setBorder(BorderFactory.createEmptyBorder(10,30,10,30));
frame.setContentPane(panel);
//Display the window.
frame.pack();
frame.setVisible(true);
JFrame.setDefaultLookAndFeelDecorated(true);
//Display the window.
//frame.pack();
//frame.setVisible(true);
}//startGUI
public void actionPerformed(ActionEvent e)
{
engine.manipulateUrl(urlInput.getText());
}
public void handle(Object event)
{
HashMap result = (HashMap) event;
Iterator urls = result.keySet().iterator();
while(urls.hasNext())
{
String url = (String) urls.next();
String status = (String) result.get(url);
System.out.println(status + " : " + url);
model.updateURLStatus(url,status);
}
}
public static void main(String[] args) throws Exception
{
WebMonitorGui checker = new WebMonitorGui(true);
checker.startGUI();
engine.start();
}
}//WebMonitorGui
Now the problem is this when i enter a url in text field it is checked after specific time but it is not shown and updated in JTable. Please telme anything i can do about it.
regards,

