JTable row selection Problem

I am facing a problem with JTable.

This what I'm doing...

I'm designing a downloader application. Which maintain a queue of downloads in JTable. For each download there will be thread created and these thread will update the table as download proceed. (No. of bytes updated or status of the download).

So I have a option which will allow me to pause a particular download.

So I have a pause Button. If a user click a particular download (by selecting a row in JTable) and if the download is already done, then Pause button has to be disable. But my problem is when ever i click the JTable button the rowselected index is -1. But when no thread is updating the JTable i'm getting the correct rowselected value.

try{

while(status==1){

//System.out.println(">>" + Utilities.compareToCurrentTime(stuff.eDate));

read_at_single = fin.read(b,0,1024);

//System.out.println(read_at_single);

if(read_at_single<0){// last packet or end

status = 5;

this.stuff.status = Integer.toString(status);

this.stuff.infoMsg ="Completed";

DownloaderGUI.tableModel.fireTableDataChanged();

}else{

if(downloadLimit>0 && total_read+read_at_single>downloadLimit){

//System.out.println("Limit : " + downloadLimit + "; total_read: " + (total_read+read_at_single));

//System.out.println("err:" + (int) (total_read+read_at_single-downloadLimit));

fout.write(b,0,(int) (downloadLimit-total_read));

total_read = total_read + (int) (downloadLimit-total_read);

downloadDone = total_read;

this.stuff.completed = Long.toString(downloadDone);

status = 5;// force completion since downloading

this.stuff.status = Integer.toString(status);

DownloaderGUI.tableModel.fireTableDataChanged();

// only a part of a file...

}elseif(isScheduled && Utilities.compareToCurrentTime(stuff.eDate)<=0){

//System.out.println("Limit : " + downloadLimit + "; total_read: " + (total_read+read_at_single));

//System.out.println("err:" + (int) (total_read+read_at_single-downloadLimit));

fout.write(b,0,read_at_single);

total_read = total_read + read_at_single;

downloadDone = total_read;

this.stuff.completed = Long.toString(downloadDone);

status = 3;// force pause since downloading only on schedule

this.stuff.status = Integer.toString(status);

this.stuff.infoMsg ="Paused:Out of Schedule";

DownloaderGUI.tableModel.fireTableDataChanged();

}

else{

fout.write(b,0,read_at_single);

total_read = total_read + read_at_single;

downloadDone = total_read;

this.stuff.completed = Long.toString(downloadDone);

DownloaderGUI.tableModel.fireTableDataChanged();

}

}

}

This is the code snippet in the downloader thread.

Hope I made by problem clear.

Thanks...

[3975 byte] By [chaos_begins_herea] at [2007-10-3 9:31:37]
# 1

Random bits of code don't really help with the problem.

Although I did notice the following:

DownloaderGUI.tableModel.fireTableDataChanged();

You should not be firing the TableDataChanged event yourself. This is the responsibility of the TableModel. You should update the data in the TableModel and it will fire the appriate event.

This event will cause the entire TableColumnMode and TableColumns to be recreated in the table resulting in the loss of the current selection.

You logic should be to update only the data for a specific row in the table using the setValueAt(...) method. Then only the cells that have changed will be repainted. Something like this:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=597870

camickra at 2007-7-15 4:46:27 > top of Java-index,Desktop,Core GUI APIs...