JProgressBar 更新
有人可以帮助我吗?我将不胜感激。 我有示例代码:
....
int sizeFile;
RandomAccessFile raf;
InputStream in;
int val= 0;
int downloaded= 0;
while((val=in.read(buff)) != -1)
{
raf.write(buff, 0, val);
downloaded+= val;
float wartosc = ((float) downloaded/ sizeFile) * 100;
prog.setValue((int)wartosc);
}
我的问题是 jprogressbar 如何放入单元格表中,更新变量 wartosc ?
Could someone help me ? I would be grateful.
I've got example code:
....
int sizeFile;
RandomAccessFile raf;
InputStream in;
int val= 0;
int downloaded= 0;
while((val=in.read(buff)) != -1)
{
raf.write(buff, 0, val);
downloaded+= val;
float wartosc = ((float) downloaded/ sizeFile) * 100;
prog.setValue((int)wartosc);
}
My question is how jprogressbar put in cell table, update variable wartosc ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JTable 的表模型应该有一个“下载进度”列,保存下载百分比值(即 0 到 100 之间的数字)。
您应该将自定义表格单元格渲染器关联到此列。渲染器将使用进度条来显示表格单元格中包含的百分比(即
TableCellRenderer
唯一方法的value
参数)。要更新进度条,您应该为表模型中的相应单元格设置新值。然后,此更改将触发
TableModelEvent
(它是通过DefaultTableModel
自动完成的,但如果您要子类化,则必须调用
)。该事件将被 JTable“捕获”,JTable 将刷新该值,从而使用要显示的新值调用渲染器。fireTableCellUpdated
抽象表模型阅读有关表的 Swing 教程。
The table model of your JTable should have a column "download progress", holding the download percentage value (i.e. a number between 0 and 100).
You should associate a custom table cell renderer to this column. The renderer would use a progress bar to display the percentage contained in the table cell (i.e. the
value
argument of the unique method ofTableCellRenderer
).To update the progress bar, you should set a new value for the appropriate cell in the table model. This change will then fire a
TableModelEvent
(it's done automatically with aDefaultTableModel
, but you have to callfireTableCellUpdated
if you're subclassingAbstractTableModel
). The event will be "caught" by the JTable which will refresh the value and thus call your renderer with the new value to display.Read the swing tutorial about tables.
不完全确定我理解你的问题,但这里有一些开始...
假设你没有在调度线程上进行下载(这将是一个坏主意)以下调用:
可能需要包含在
SwingUtilities.invokeLater
。这是因为Swing是线程不安全的,Swing框架的对象需要从单个线程访问。
Not entirely sure I understand your question, but here's something to start with...
Assuming you're not doing downloads on the dispatch thread (which would be a bad idea) the following call:
probably needs to be wrapped in a
SwingUtilities.invokeLater
.This is because Swing is thread unsafe and the object of the Swing framework needs to be accessed from a single thread.