为什么在JTable列中没有显示动画.gif图标? [英] Why is an animated .gif icon not showing in JTable column?
问题描述
这是processing.gif
Here is the processing.gif
这是initial.png
Here is initial.png
这是输出
这是代码。 processing.gif正在其他位置工作,例如 JTabbedPane
的选项卡中。在 JTable
的列中,它没有显示。任何解释和解决方案? processing.gif是一个移动图标,表示正在加载某些内容。
Here is the code. processing.gif is working in other locations such as in the tab of a JTabbedPane
. Here in the column of a JTable
, it is not showing. Any explanation and solution? processing.gif is a moving icon that indicates that something is loading.
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JFrame
{
public TableIcon()
{
ImageIcon initial = new ImageIcon(getClass().getResource("initial.png"));
ImageIcon processing = new ImageIcon(getClass().getResource("processing.gif"));
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{initial, "initial"},
{processing, "processing"}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
推荐答案
动画gif在JTable中默认不能正常工作。但有一种简单的方法可以解决这个问题,使用<$ h $ =http://www.qedeq.org/0_04_07/src/ reports / qedeq_gui_se / source / furbelow / AnimatedIcon.java.htmlrel =nofollow>这里
Animated gif's don't work well by default in JTable. But there is an easy way to fix this, use the AnimatedIcon
class that can be found here
基本上,它重新实现图标
界面,注册你渲染图标的位置,当需要绘制新的gif框架时,它会自动重绘正确的区域。
Basically, it reimplements Icon
interface, register where you rendered the icon, and when a new frame of the gif needs to be painted, it will automatically repaint the correct area.
此处提供另一种选择需要渲染动画gif的每个单元格的特定ImageObserver,但我发现它更乏味。
There is another alternative provided here where you register a specific ImageObserver for each cell that needs to render an animated gif, but I find it a bit more tedious.
这篇关于为什么在JTable列中没有显示动画.gif图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!