Why is the animated .gif icon not showing in the JTable column?

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

Here is initial.png Here is the initial.png

Here is the output Here is the conclusion

Here is the code. processing.gif works elsewhere, such as on a tab JTabbedPane. Here in column a JTableit is not displayed. Any explanations and solutions? processing.gif is a moving icon indicating 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);
    }

}
+4
source share
2 answers

Animated gifs by default do not work in JTable. But there is an easy way to fix this, use the class AnimatedIconthat can be found here

, Icon, , , gif , .

, ImageObserver , gif, .

+4

, GIF JTable, , , . , JTabbedPane. TableIcon:

getContentPane().add(new JLabel(processing), BorderLayout.SOUTH);

: GIF . GIF . , . , , , ( TableIcon):

final Timer animationTimer = new Timer(100, e -> table.repaint());
animationTimer.start();
0

Source: https://habr.com/ru/post/1584550/


All Articles