这是processing.gif
这是initial.png
这是输出
这是代码。 processing.gif正在其他位置工作,例如JTabbedPane
的标签中。在JTable
的列中,它没有显示。任何解释和解决方案? processing.gif是一个移动图标,表示正在加载某些内容。
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);
}
}
答案 0 :(得分:4)
动画gif在JTable默认情况下效果不佳。但是有一种简单的方法可以解决这个问题,使用可以找到的AnimatedIcon
类here
基本上,它重新实现Icon
界面,注册你渲染图标的位置,当需要绘制新的gif框架时,它会自动重绘正确的区域。
还提供了另一个替代方案here,您可以为需要渲染动画gif的每个单元注册一个特定的ImageObserver,但我发现它更乏味。
答案 1 :(得分:0)
如果JTable
完全支持动画GIF会很棒,但不幸的是它看起来不像。您已经提到它可以在JTabbedPane
的标签中使用。我将以下行添加到TableIcon
构造函数的末尾:
getContentPane().add(new JLabel(processing), BorderLayout.SOUTH);
这稍微改变了这种情况:该表显示了GIF的一帧而不是任何内容。动画GIF正在标签中工作。我还注意到,当您调整窗口大小时,表中的动画正在运行。这给了我这个脏黑客的想法,它似乎在我的系统上工作(也添加到TableIcon
构造函数的末尾):
final Timer animationTimer = new Timer(100, e -> table.repaint());
animationTimer.start();