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,

[7198 byte] By [GameOvera] at [2007-11-27 0:39:50]
# 1

you can use any of the fire methods from the AbstractTableModel class

i have used

fireTableDataChanged() //if the data gets changed

fireTableStructureChanged() //if row count or column count gets changed

please go through the API of AbstractTableModel.

and please give feedback whether it has worked or not

Tapan Maru

Sun Certified Java Programmer

tapan.javaprogrammer@gmail.com

tapan_javaprogrammera at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the reply

no it didn't work

i have tried so many things

i think it is not refreshing the GUI because when i tried this

model.updateURLStatus("test","test");

JTable result = new JTable(model);

result.setPreferredScrollableViewportSize(new Dimension(250, 50));

JScrollPane scrollPane = new JScrollPane(result);

it worked and was shown in table

GameOvera at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

you need to pass the reference of WebMonitorGui into WebMonitorEngine and set ob to that reference. it is nothing but a referenceing problem.

if you do not understand then please get back and i will try to provide the solution.

i hope you understand what do i mean.

regards

Aniruddha

Aniruddha-Herea at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 4
do not mind tapan, you need to study the code. just being "Sun Certified Java Programmer", you should not provide this kind of solution. before you provide any solution study the code and then give your opinion. you have never gone through the code.
Aniruddha-Herea at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi Aniruddha

Thank you for prompt response

It worked. I understood what you wanted me to do and i did that and it started working. It is true that in front me the GUI displayed was because of one instance and i was trying to update the table by another instance so it didn't work. am i right?

regards,

GameOvera at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 6
yes you got it..best of luck....
Aniruddha-Herea at 2007-7-11 22:52:19 > top of Java-index,Desktop,Core GUI APIs...