Issue
Here is the processing.gif
Here is initial.png
Here is the output
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);
}
}
Solution
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
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.
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.
Answered By - Guillaume Polet Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.