본문 바로가기

Swing/JTable 10

JTable GIF Anim

JTable 의 셀에서 GIF 애니메이션 구현하는 예


다음과 같은 테이블 모델 클래스를 작성하고 JTable.setModel() 등으로 전달해주면 된다

아래 코드의 20 ~ 39 라인이 반드시 포함되어야만 GIF 애니메이션이 작동되는 것을 확인할 수 있다


package jtable;

import java.awt.*;
import java.awt.image.*;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.table.*;

class MyTableModel extends AbstractTableModel 
{
    boolean DEBUG = true;
    ImageIcon [] icon = new ImageIcon[2];
    
    private String[] columnNames;
    private Object[][] data;

    MyTableModel(JTable table) {

        icon[0] = new ImageIcon(getClass().getResource("lineslink.gif"));
        icon[1] = new ImageIcon(getClass().getResource("animated-dot.gif"));
        
        for(int i=0;i<icon.length;i++) {
	        icon[i].setImageObserver(new ImageObserver() {
	        	// 이미지가 변경될 때마다 호출되는 메소드
	            @Override public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {

	            	if (!table.isShowing()) {
	                    return false;
	                }
	                if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
	                	for(int i=0;i<table.getRowCount();i++) {
	                		table.repaint(table.getCellRect(i, 3, false));
	                	}
	                }
	                return (infoflags & (ALLBITS | ABORT)) == 0;
	            }
	        });
        }
        
        columnNames = new String[]{"EMPNO","ENAME","HIREDATE","PICTURE","SELECT"};
        
        Calendar cal = Calendar.getInstance();
        
        data = new Object[5][];
        cal.set(2001, 0, 12);
        data[0] = new Object[]{ 5, "SMITH",new Date(cal.getTimeInMillis()), icon[0], new Boolean(false) };
        cal.set(2002, 2, 16);
        data[1] = new Object[]{ 3, "JONES",new Date(cal.getTimeInMillis()), icon[1], new Boolean(true) };
        cal.set(2003, 3, 21);
        data[2] = new Object[]{ 4, "WARD",new Date(cal.getTimeInMillis()), icon[0], new Boolean(false) };
        cal.set(2005, 5, 24);
        data[3] = new Object[]{ 6, "FORD",new Date(cal.getTimeInMillis()), icon[1], new Boolean(true) };
        cal.set(2010, 9, 27);
        data[4] = new Object[]{ 7, "MARY",new Date(cal.getTimeInMillis()), icon[0], new Boolean(false) };
    }
        
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

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

    // 한개의 셀을 그릴 때마다 호출됨
    public Object getValueAt(int row, int col) {
        //System.out.print("getValueAt("+row+","+col+")");
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        return col==1 || col==4 ? true : false;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
    */
    // JTable의 셀을 편집하고 엔터를 치면 호출되는 메소드
    public void setValueAt(Object value, int row, int col) {
        System.out.println("테이블모델.setValueAt("+value+","+row+", "+col+")"); 
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

        data[row][col] = value;

        fireTableCellUpdated(row, col);//모든 리스너에게 셀 데이터 변경을 알린다

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}