PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] Why is an animated .gif icon not showing in JTable column?

 July 18, 2022     gif, icons, jtable, swing, user-interface     No comments   

Issue

Here is the processing.gif Here is the processing.gif

Here is initial.png Here is initial.png

Here is the output 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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing