Animated Icon Header Example : Grid Table « Swing Components « Java

Animated Icon Header Example


// Example from

/* (swing1.1.1beta2) */

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;

 * @version 1.0 06/19/99
public class AnimatedIconHeaderExample extends JFrame {

  public AnimatedIconHeaderExample() {
    super("AnimatedIconHeader Example");

    final Object[][] data = new Object[][] { { "Leopard", "Lycaon" },
        { "Jagur", "Jackal" }, { "Cheetah", "Coyote" },
        { "Puma", "Dingo" }, { "Lynx", "Fox" }, { "Tom", "Hot" } };
    final String[] column = new String[] { "Cat", "Dog" };

    ImageIcon[] icons = { new ImageIcon("Java2sAnimation.gif"),
        new ImageIcon("Java2sAnimation.gif") };

    AbstractTableModel model = new AbstractTableModel() {
      public int getColumnCount() {
        return column.length;

      public int getRowCount() {
        return data.length;

      public String getColumnName(int col) {
        return column[col];

      public Object getValueAt(int row, int col) {
        return data[row][col];

    JTable table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    JLabel renderer;
    for (int i = 0; i < model.getColumnCount(); i++) {
      renderer = (JLabel) table.getColumn(column[i]).getHeaderRenderer();

      // If you have only one column.
      // icons[i].setImageObserver(header);

      icons[i].setImageObserver(new HeaderImageObserver(header, i));
    JScrollPane pane = new JScrollPane(table);

  class HeaderImageObserver implements ImageObserver {
    JTableHeader header;

    int col;

    HeaderImageObserver(JTableHeader header, int col) {
      this.header = header;
      this.col = col;

    public boolean imageUpdate(Image img, int flags, int x, int y, int w,
        int h) {
      if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
        Rectangle rect = header.getHeaderRect(col);
      return (flags & (ALLBITS | ABORT)) == 0;

  public static void main(String[] args) {
    AnimatedIconHeaderExample frame = new AnimatedIconHeaderExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
    frame.setSize(300, 140);

Related examples in the same category

1.HyperLink in TableHyperLink in Table
2.Column popup menuColumn popup menu
3.Tree TableTree Table
4.Swing Table in ComboBoxSwing Table in ComboBox
5.Tabbable Currency TableTabbable Currency Table
6.Icon Currency TableIcon Currency Table
7.MultiLine Header TableMultiLine Header Table
8.ToolTip TableToolTip Table
9.Striped Currency TableStriped Currency Table
11.Calculated Column TableCalculated Column Table
12.Table Utilities
13.Fraction Currency TableFraction Currency Table
14.Highlight Currency TableHighlight Currency Table
15.Highlight Currency Table 2Highlight Currency Table 2
16.MultiLine TableMultiLine Table
17.Updatable Highlight Currency TableUpdatable Highlight Currency Table
18.Editable Highlight Currency TableEditable Highlight Currency Table
19.ComboBox TableComboBox Table
20.Groupable(Group) Header ExampleGroupable(Group) Header Example
21.MultiWidth Header ExampleMultiWidth Header Example
22.MultiLine Header ExampleMultiLine Header Example
23.Table Row Header ExampleTable Row Header Example
24.Fixed Table Column ExampleFixed Table Column Example
25.Button Table ExampleButton Table Example
26.Radio Button Table ExampleRadio Button Table Example
27.RadioButton Table Example 2RadioButton Table Example 2
28.MultiLine Cell ExampleMultiLine Cell Example
29.Each Row with different Editor ExampleEach Row with different Editor Example
30.Multiple Component Table: Checkbox and ComboboxMultiple Component Table: Checkbox and Combobox
31.multiple Component Table 2: checkboxmultiple Component Table 2: checkbox
32.Union Data Table ExampleUnion Data Table Example
33.Total(Calculate) Row ExampleTotal(Calculate) Row Example
34.Colored Cell Table Example
35.multiple Font Cell Table Example
36.Multi Span Cell Table Example
37.Mixed Table Example
38.Pushable Table Header ExamplePushable Table Header Example
39.Sortable Table ExampleSortable Table Example
40.ToolTip Header Table ExampleToolTip Header Table Example
41.Indicator Table ExampleIndicator Table Example
42.Fixed Table Row ExampleFixed Table Row Example
43.multiple Row Header Example
44.Column Border Table ExampleColumn Border Table Example
45.Cell Border Table ExampleCell Border Table Example
46.Hide Column Table ExampleHide Column Table Example
47.Animated Icon Table ExampleAnimated Icon Table Example
48.Editable Header Table ExampleEditable Header Table Example
49.Editable Header Table Example 2Editable Header Table Example 2