1

I'm encountering a bit of trouble here.

My purpose is to add GIF image into a Button, and send that button into the JTable.

First of all, I made a jtable with customized code.

private javax.swing.JTable Tbl_Monkey = new javax.swing.JTable();

Tbl_Monkey.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null, null, null, null, null, null, null, null, null, null}
    },
    new String [] {
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
    }
) {
    boolean[] canEdit = new boolean [] {
        true, false, false, false, false, false, false, false, false, false, false, false, false
    };

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return JButton.class;
    }
});


Tbl_Monkey.setDefaultRenderer(JButton.class, new JButtonTableRenderer());

And also I made its customized JTable renderer.

public class JButtonTableRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {

        return (JButton) value;


    }
}

After that, I set the Icon (GIF) of the JButton then passing it right into the JTable by using DefaultTableModel, And it fits perfectly into the JTable, but the animated GIF is not animating.

  DefaultTableModel  oDtb = (DefaultTableModel) Tbl_Monkey.getModel();
   oDtb.addRow(oTheObjectArrayofJButton);

I realized if I just make the JButton outside of the JTable, the Icon (GIF) would animates. But if I put the JButton inside the JTable, it doesn't animate. Unless if I click that Button... then the animation came, but that only 1 frame. I should re-click to get the animation of that GIF on the clicked button. THat's not good....

How to solve it out?

gumuruh
  • 2,544
  • 4
  • 33
  • 56
  • 1
    my question about animated Gif, in case if you put these file as Icon to the JButton outside JTable then animations works ??? – mKorbel Sep 24 '11 at 11:12
  • @mKorbel yes? The JButton has no problem if I use the setIcon() method. The problem is on the JTable only.... seems JTable only taking JButton as a static JButton. So, whenever I have the JButton with its Icon placed inside the JTable, the JTable didn't render the animation very well.... any ideas why it behave like this? If I put the GIF outside the JTable, yes... it works. Eventough If I make 1 JButton with GIF as its Icon, and Placed it outside the JTable, it does animating. – gumuruh Sep 25 '11 at 02:46
  • then check this code http://tips4java.wordpress.com/2008/12/18/icon-table-cell-renderer/ , put here only animated Icon, then we'll see ... – mKorbel Sep 25 '11 at 07:22
  • thanks @mKorbel, that's a long code. actually I'm a bit confused. Isn't "getTableCellRendererComponent()" method with its "return (JButton) value;" is enough for rendering the JButton? The code you gave from the links, seems returning other value. :( – gumuruh Sep 29 '11 at 00:47

1 Answers1

2

I believe the cause of your problem is that JTable use JButton to render the cell, unless you are editing the given cell which then makes JTable use JButton as the cell editor.

When JTable use JButton as cell renderer, it probably just tells JButton to draw itself in the given cell, and that's it. I'm not quite sure which approach SWING takes with regard to drawing the animated GIF on JButtons, but i would think that there is a thread doing some background work. However, in the case of JTable, we're not dealing with a real button and thus it's not repainted.

I don't have a true and tried suggestion as to how you can solve your problem other than recommending another post which goes on about nearly the same issue.

Community
  • 1
  • 1
sbrattla
  • 5,274
  • 3
  • 39
  • 63
  • ya i think you're right. The swing probably the one who do the work of repainting the image of the JButton. But that's not for the case if the JButton is placed on the JTable.... omg... another cases. Does JDK Developer team, never think about this anyway? :D – gumuruh Sep 25 '11 at 02:49
  • The default cell renderer of a JTable uses a JLabel, not a JButton. Look at DefaultTableCellRenderer. – gouessej Apr 04 '13 at 12:19
  • Yes, but I believe gumuruh passes a JButton to the JTable, which would result in the JButton being used as a cell renderer. Gumuruh writes that "[...] I set the Icon (GIF) of the JButton then passing it right into the JTable [...]", which led me to this conclusion. – sbrattla Apr 05 '13 at 18:35