Witam
Mógłby mi ktoś pomóc z poniższym kodem:
Problem polega na tym, że komórka w postaci progressbara w ogóle nie wypełnia się
w trakcie pobierania pliku. Co jeszcze mam dodać do tego kodu aby wreszcie zadziałał ?
public class Test extends JFrame
{
public static void main(String[] args)
{
new Test().setVisible(true);
}
public Test()
{
setSize(500,400);
mod = new ModelTab();
tab =new JTable(mod);
tab.setDefaultRenderer(JProgressBar.class, ren);
add(new JScrollPane(tab));
}
JTable tab;
ModelTab mod;
Render ren = new Render();
}
class Downloader implements Runnable
{
String link = "http://ember.cdburnerxp.se/cdbxp_setup_4.3.8.2631.exe";
String dir = "C:\\abc\\";
public int downloaded=0;
public int sizeFile = -1;
public Downloader()
{
Thread t = new Thread(this);
t.start();
}
public void run()
{
getFile();
}
public void getFile()
{
int kB = 1024;
byte[] buff = new byte[kB];
try
{
URL url = new URL(link);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.connect();
sizeFile = this.getSIZE(http);
File file = new File(this.getFileName(url));
RandomAccessFile raf = new RandomAccessFile(dir+file ,"rw");
raf.seek(downloaded);
InputStream in = http.getInputStream();
int reading = 0;
while((reading=in.read(buff)) != -1)
{
if(sizeFile - downloaded <= kB)
{
buff = new byte[sizeFile - downloaded];
}
raf.write(buff, 0, reading);
**downloaded **+= reading ; // zmienna wysłana do modelu tabeli
}
raf.close();
in.close();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
String linkHTTP()
{
return link;
}
String getFileName(URL url)
{
String fileName = url.getFile();
return fileName.substring(fileName.lastIndexOf('/') + 1);
}
public int getSIZE(HttpURLConnection huc)
{
return huc.getContentLength();
}
}
class ModelTab extends AbstractTableModel
{
public ModelTab()
{
list.add(new Downloader());
}
String[] colName = {"Link","Size","Progres"};
Class[] typCol = {String.class, Integer.class, JProgressBar.class};
public ArrayList list = new ArrayList();
public int getColumnCount()
{
return colName.length;
}
public String getColumnName(int col)
{
return colName[col];
}
public Class getColumnClass(int col)
{
return typCol[col];
}
public int getRowCount()
{
return list.size();
}
public Object getValueAt(int row, int column)
{
Downloader d = (Downloader) list.get(row);
switch (column)
{
case 0:
return d.linkHTTP();
case 1:
return d.sizeFile;
case 2:
return d.**downloaded**;
}
return "";
}
public void setValueAt(Object val, int row, int column)
{
Downloader d = (Downloader)list.get(row);
switch(column)
{
case 2: d.**downloaded **= (Integer)val;
}
fireTableCellUpdated( row, column);
}
}
class Render extends JProgressBar implements TableCellRenderer
{
public Render()
{ setMinimum(0);
setMaximum(100);
setStringPainted(true);
}
public Component getTableCellRendererComponent(JTable arg0, Object value,
boolean arg2, boolean arg3, int arg4, int arg5)
{
setValue((Integer) value);
return this;
}
}