动画的JTable [英] animation in JTable

查看:206
本文介绍了动画的JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把GIF图像(动画),以一个JTable细胞,但它没有被显示出来。正如我已阅读,JTable组件是静态的,它需要重写渲染。

问题是,我有一些数据,并将其粘贴到计算表格单元格一个线程,在计算我想显示一些旋转的车轮。此外,我从另一个单独的线程行添加到表中。所以,一个线程增加了一排的一些数据,而另一个线程的行中的单元格计算的数据。

是否有可能增加一个动画图标到同一个小区中的每个添加的行吗?有没有人的想法怎么办呢?

UPD:
现在,我可以创建gif动画一排,但不能这样行的表从一个线程添加

 公共类AnimatedIconTableExample扩展的JFrame {    私有静态最后的serialVersionUID长= -1038271613549995183L;    公共AnimatedIconTableExample(){
        超级(AnimatedIconTable实施例);        最终目标[] []数据=新对象[] [] {
                {,,新的ImageIcon(SRC / loading.gif),
                        文本}};
        最终目标[] =列新的对象[] {第一,第二,第三,第四};        的DefaultTableModel模型=新的DefaultTableModel(){            私有静态最后的serialVersionUID长= 31150076182704312L;            公众诠释getColumnCount(){
                返回column.length;
            }            公众诠释getRowCount(){
                返回data.length;
            }            公共字符串getColumnName(INT COL){
                返回(字符串)列[COL];
            }            公共对象getValueAt(INT行,诠释山口){
                返回数据[行] [COL];
            }            公共类<>的getColumnClass(INT COL){
                返回ImageIcon.class;
            }
        };        JTable的表=新的JTable(模型);
        setImageObserver(表);
        JScrollPane的窗格=新JScrollPane的(表);
        。的getContentPane()加(面板);        的for(int i = 0;我小于5;我++){
            新TableUpdater(模型,数据)。开始();
        }    }    私人无效setImageObserver(JTable的表){
        TableModel的模型= table.getModel();
        INT colCount = model.getColumnCount();
        INT rowCount等= model.getRowCount();
        对于(INT COL = 0;&山坳下,colCount;西++){
            如果(ImageIcon.class == model.getColumnClass(COL)){
                对于(INT行= 0;&行LT; rowCount等;排++){
                    obj对象= model.getValueAt(行,列);
                    ImageIcon的图标= NULL;
                    如果(OBJ的instanceof的ImageIcon)
                        图标=(ImageIcon的)model.getValueAt(行,列);
                    如果(图标!= NULL){
                        icon.setImageObserver(新CellImageObserver(表,行,
                                COL));
                    }
                }
            }
        }
    }    类CellImageObserver实现ImageObserver的{
        JTable的表;
        INT行;
        INT关口;        CellImageObserver(JTable的表,诠释行,诠释山口){
            this.table =表;
            this.row =排;
            this.col =关口;
        }        公共布尔的imageUpdate(图片IMG,诠释旗帜,INT X,INT Y,INT W,
                INT高){
            如果((标志及(FRAMEBITS | ALLBITS))!= 0){
                矩形RECT = table.getCellRect(行,列,FALSE);
                table.repaint(RECT);
            }
            返回(旗及(ALLBITS | ABORT))== 0;
        }
    }    公共静态无效的主要(字串[] args){
        AnimatedIconTableExample帧=新AnimatedIconTableExample();
        frame.addWindowListener(新WindowAdapter的(){
            公共无效的windowClosing(WindowEvent五){
                System.exit(0);
            }
        });
        frame.setSize(300,150);
        frame.setVisible(真);
    }
}公共类TableUpdater继承Thread {    私人的DefaultTableModel模型;
    私有对象[] []的数据;    公共TableUpdater(的DefaultTableModel模型,对象[] []数据){
        this.model =模型;
        this.data =数据;
    }    公共无效的run(){
        尝试{
            视频下载(3000);
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }
        model.addRow(数据);
    }
}


解决方案

阅读有关的编辑器和渲染器

的JTable 允许你把图片和图标。

您需要重写你的的getColumnClass 方法。

I am trying to put gif image(animated) to a JTable cell, but it's not being displayed. As I have read, JTable component is static and it's required to rewrite rendering.

The point is that I have a thread which calculates some data and pastes it to a table cell, while calculating I want to display some rotating wheel. Moreover, I add rows to the table from another separate thread. So, one thread adds a row with some data and another thread calculates data for a cell in the row.

Is it possible to add an animation icon into the same cell in each added row? Has anyone ideas how to do it?

Upd: Now I can create a row with animated gif, but can't add such rows to a table from a thread

public class AnimatedIconTableExample extends JFrame {

    private static final long serialVersionUID = -1038271613549995183L;

    public AnimatedIconTableExample() {
        super("AnimatedIconTable Example");

        final Object[][] data = new Object[][] {
                { "", "", new ImageIcon("src/loading.gif"),
                        "text" } };
        final Object[] column = new Object[] { "First", "Second", "Third", "Fourth" };

        DefaultTableModel model = new DefaultTableModel() {

            private static final long serialVersionUID = 31150076182704312L;

            public int getColumnCount() {
                return column.length;
            }

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

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

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

            public Class<?> getColumnClass(int col) {
                return ImageIcon.class;
            }
        };

        JTable table = new JTable(model);
        setImageObserver(table);
        JScrollPane pane = new JScrollPane(table);
        getContentPane().add(pane);

        for (int i = 0; i < 5; i++) {
            new TableUpdater(model, data).start();
        }

    }

    private void setImageObserver(JTable table) {
        TableModel model = table.getModel();
        int colCount = model.getColumnCount();
        int rowCount = model.getRowCount();
        for (int col = 0; col < colCount; col++) {
            if (ImageIcon.class == model.getColumnClass(col)) {
                for (int row = 0; row < rowCount; row++) {
                    Object obj = model.getValueAt(row, col);
                    ImageIcon icon = null;
                    if (obj instanceof ImageIcon)
                        icon = (ImageIcon) model.getValueAt(row, col);
                    if (icon != null) {
                        icon.setImageObserver(new CellImageObserver(table, row,
                                col));
                    }
                }
            }
        }
    }

    class CellImageObserver implements ImageObserver {
        JTable table;
        int row;
        int col;

        CellImageObserver(JTable table, int row, int col) {
            this.table = table;
            this.row = row;
            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 = table.getCellRect(row, col, false);
                table.repaint(rect);
            }
            return (flags & (ALLBITS | ABORT)) == 0;
        }
    }

    public static void main(String[] args) {
        AnimatedIconTableExample frame = new AnimatedIconTableExample();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setSize(300, 150);
        frame.setVisible(true);
    }
}

public class TableUpdater extends Thread {

    private DefaultTableModel model;
    private Object[][] data;

    public TableUpdater(DefaultTableModel model, Object[][] data) {
        this.model = model;
        this.data = data;
    }

    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        model.addRow(data);
    }
}

解决方案

Read about Editors and Renderers.

JTable allows you to put Images and Icons.

You need to override your getColumnClass method.

这篇关于动画的JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆