1,105,225 Community Members

JTABLE...

Member Avatar
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

hello!

i need to create JTable which swing string and animated gif image also...

how to put animated gif and data(string) in Jtable cell dynamically...

can you give any referel link or codes...

i tried lot but vain...

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

hello!
i need to create JTable which swing string and animated gif image also...
how to put animated gif and data(string) in Jtable cell dynamically...
can you give any referel link or codes...
i tried lot but vain...

What have you done so far?
http://www.daniweb.com/forums/thread251668.html

Member Avatar
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

here the code i done it...

the animation gif got placed in cell but data how do i put it along with gif..

like

--------------------------------------
coloumn1 | column2 | column 3
-------------------------------------
name | place | animatedgif(like rotating ring)
_____________________________

/** 
* tableExample.java 
* This class shows how to create a simple JTable 
* using table model 
* 
* author Kanad Deshpande
*/ 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;

import javax.swing.table.*;

import java.util.*;
import javax.swing.event.*;



public class  StatusTable implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
@SuppressWarnings("unchecked")
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;

JPanel mainPanel,buttonPanel;
   public static void main(String[] args) 
    {
	   @SuppressWarnings("unused")
	StatusTable t=new StatusTable();
    }

@SuppressWarnings("unchecked")
StatusTable()
{

   

    final Object[][] data = new Object[][] {
        { 
        	("qwer"),
        	("qwer"),
        	("qwer"),
            new ImageIcon("C:\\Temp\\b020.gif")}//atr.gif
        };
    final Object[] column = new Object[] { "No", "FirstName","LastName","Status" };

    AbstractTableModel model = new AbstractTableModel() {
      /**
		 * 
		 */
		private static final long serialVersionUID = 1L;


	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;
      }
    };
	
  
	
	
	
rows=new Vector();
columns= new Vector();
String[] columnNames = 
{ 
"No", 
"Name",
"MailId",
};
addColumns(columnNames);

//tabModel=new DefaultTableModel();
//tabModel.setDataVector(rows,columns);

table = new JTable(model);
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(false);

table.getModel().addTableModelListener(this);

lblMessage=new JLabel("");


buttonPanel=new JPanel();



mainPanel=new JPanel();
frame=new JFrame("Simple Table Example");
frame.setSize(400,300);
setImageObserver(table);
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",scrollPane);
//mainPanel.add("South",buttonPanel);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);

}
 @SuppressWarnings("unchecked")
public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}

@SuppressWarnings("unchecked")
public void addRow(Vector v) //Add Row
{

//r=createBlankElement();
rows.addElement(v);

table.addNotify();

}

 void deleteRow(int index) 
   {
     if(index!=-1)//At least one Row in Table
      { 
        rows.removeElementAt(index);
        table.addNotify();
       }

   }//Delete Row

 public void tableChanged(javax.swing.event.TableModelEvent source)     {
                 String msg="";
                 @SuppressWarnings("unused")
				TableModel tabMod = (TableModel)source.getSource();
                 	switch (source.getType())
                 	{
                 		case TableModelEvent.UPDATE:
                 		msg="Table Value Updated for  cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
                        JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
                        break;
                 		}

    }//Table Changed Method

public void selectCell(int row,int col)
    {
     if(row!=-1 && col !=-1)            
     {
      table.setRowSelectionInterval(row,row);
      table.setColumnSelectionInterval(col,col);
      }
    }

@Override
public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub
	
}

/*public void actionPerformed(ActionEvent source)
    {
        
             addRow();
         
        
    
    }*///ActionList


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++) {
    if((rowCount-1)==row){
          ImageIcon icon = (ImageIcon) model.getValueAt((rowCount-1), (colCount-1));
          icon.setImageObserver(new CellImageObserver(table,(rowCount-1), (colCount-1)));
         
          if (icon != null) {
          
        	  icon.setImageObserver(new CellImageObserver(table,(rowCount-1), (colCount-1)));
          }
    }
   
       }
      }
    }
  }



  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;
    }
  }
}
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

First of all about this method:

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

You define data:

final Object[][] data = new Object[][] {
{
  ("qwer"),
  ("qwer"),
  ("qwer"),
  new ImageIcon("C:\\Temp\\b020.gif")}//atr.gif
};

The first columns are String and the last is ImageIcon. But your method always returns ImageIcon.class. That method needs to return the class of the object that each column has. You need to do something like this:

public Class getColumnClass(int col) { 
   if (col==3) return ImageIcon.class; // column 3 has ImageIcon
   return "".getClass(); // the rest are Strings
}

Or better, assuming that data has at least one row:

public Class getColumnClass(int col) { 
   return data[0][col].getClass();
}

Also I noticed that you need to add columns and rows to the table. A simple example on how to add more rows. First you mustn't have data to be an array. You initialize the AbstractTableModel with an array, which is static. Meaning that is difficult to add more rows than the ones already there. If you need to add more rows then you need to make sure that the methods of the AbstractTableModel return those rows. Why don't you try this:

public class CustomTableModel extends AbstractTableModel {
    private String[] columnNames;
    private Vector<Object []> data;

    public CustomTableModel(String[] columnNames, Vector<Object []> data) {
        this.columnNames = columnNames;
        this.data = data;
    }

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

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

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object [] obj = data.get(rowIndex);
        return obj[columnIndex];
    }


    public Class getColumnClass(int columnIndex) {
        Object [] obj = data.get(0);
        return obj[columnIndex].getClass();
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }
    
    [B]public void addRow(Object [] row) {
        data.add(row);
    }[/B]
}

And call it like this:

String [] columnNames = {"Name", "Something", "Image"};
        
        Vector<Object []> data = new Vector<Object []>();

        Object [] v = {"AAAA", "BBBBBB",  icon};
        data.add(v); // first row
        
        
        // create table        
        CustomTableModel ctm = new CustomTableModel(columnNames, data);
        JTable table = new JTable(ctm);

And whenever you need to add a new row just call the add method of the "ctm". That would mean of course that ctm must be visible where you add the new row. You need to add the row at the "ctm" you used to create the table

Member Avatar
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you lot...

here i got your point...

but still i don't know the icon not appearing in cell

if it is appear then i need it be moving(like ring rotating..) not in idle...

please help for me..

package test.iecanvas;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class RecordShow extends AbstractTableModel {

	static JFrame frame;
	static JScrollPane scrollPane;
	static JPanel mainPanel;
	    private String[] columnNames;
	    private Vector<Object []> data;

	    public RecordShow(String[] columnNames, Vector<Object []> data) {
	        this.columnNames = columnNames;
	        this.data = data;
	    }

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

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

	    public Object getValueAt(int rowIndex, int columnIndex) {
	        Object [] obj = data.get(rowIndex);
	        return obj[columnIndex];
	    }


	    public Class getColumnClass(int columnIndex) {
	        Object [] obj = data.get(0);
	        return obj[columnIndex].getClass();
	    }

	    public String getColumnName(int column) {
	        return columnNames[column];
	    }
	    
	    public void addRow(Object [] row) {
	        data.add(row);
	    }
	    
	    public static void main(String args[])
	    {
	    	      String [] columnNames = {"Name", "Something", "Image"};
	    	      Vector<Object []> data = new Vector<Object []>();
	    	      Object [] v = {"AAAA", "BBBBBB",  new ImageIcon("C:\\Temp\\atr.gif")};
	    	      data.add(v); // first row
	    	      // create table
	    	      RecordShow ctm = new RecordShow(columnNames, data);
	    	      JTable table = new JTable(ctm);
	    	      scrollPane= new JScrollPane(table);//ScrollPane
	    	      
	    	      mainPanel=new JPanel();
	    	      frame=new JFrame("Simple Table Example");
	    	      frame.setSize(400,300);
	    	    
	    	      mainPanel.setLayout(new BorderLayout());
	    	      mainPanel.add("Center",scrollPane);
	    	      //mainPanel.add("South",buttonPanel);
	    	      mainPanel.setBackground(Color.white);
	    	     
	    	      table.getParent().setBackground(Color.black);
	    	      frame.getContentPane().add(mainPanel);
	    	      frame.setVisible(true);

	    }
	}
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Are you saying that the image appears but is not moving? If yes, can you attach it so I can do some tests with it?

Member Avatar
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

take this gif and test it...

Attachments b020.gif 6.22KB
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

I did some testings and when the gif is static then it displays correctly. I had a problem displaying the gif when it has motion. The image displays in the jTable, but it is not moving and when you click the cell then it changes what it displays, but it doesn't have motion. Meaning that every time I clicked another cell and then the image, the image changed what it displayed but it was not moving like it was in your post.

I tried putting it in a JLabel and simply display the label somewhere in the jPanel, outside the table and it was OK.

So 3 Solutions:
1) Use static images and put them in the table

2) Don't use table if you don't have to. You can use GridBagLayout that allows you to put components (buttons, fields, labels) organized and have one of these to be a JLabel with the moving image. Meaning that with the GridBagLayout you will place your components on the jPanel to look like they are in a table

3) Don't listen to me. Just because I couldn't do it, doesn't mean it can be done. Try to search a bit more and find how to put moving images in a table.


And one last general suggestion.

The RecordShow class you created wasn't suppose to do anything but extend the AbstractTableModel. The idea was to do only that and call it from another class. Don't mix things together. RecordShow should extend the AbstractTableModel and only.
Then create another class for the frame.

Speaking of which I find it better to have the class extend the JFrame instead of declaring it in the main. In the constructor put all that code that you used to add components to the frame.
I also added a JLabel just to show you how the moving image works

package stam.bar.dani.tableimages;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Image;

import java.awt.Toolkit;

import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class CustomFrame extends JFrame {
    private RecordShow ctm = null;
    private JTable table = null;
    private JScrollPane scrollPane = null;
    private JPanel mainPanel = null;
    
    public CustomFrame(String[] columnNames, Vector<Object []> data) {
        super("Simple Table Example");
        setSize(400,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // initialize the components
        ctm = new RecordShow(columnNames, data);
        table = new JTable(ctm);
        scrollPane= new JScrollPane(table);//ScrollPane

        mainPanel=new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add("Center",scrollPane);

        // for testing the image
        mainPanel.add("South",new JLabel(new ImageIcon("C:\\temp\\b020.gif")));

        mainPanel.setBackground(Color.white);
        
        table.getParent().setBackground(Color.black);
        
        getContentPane().add(mainPanel);
        
        // you can leave that and put it where you create the JFrame
        // setVisible(true);
    }
    
    public static void main(String [] args) {
        String [] columnNames = {"Name", "Something", "Image"};
        
        // THE FOLLOWING DATA COULD BE TAKEN FROM ANOTHER METHOD
        // ONE THAT READS A FILE OR A DATABASE
        ImageIcon ii = new ImageIcon("C:\\temp\\b020.gif");
        Vector<Object []> data = new Vector<Object []>();
        Object [] v = {"AAAA", "BBBBBB",  ii};
        data.add(v); // first row
        // <<
        
        // >>
        CustomFrame frame = new CustomFrame(columnNames, data);
        
        // You can call also that method from the constructor. It's up to you
        frame.setVisible(true);
    }
}

Also if the cell where the image is displayed seems to small you can do this, after you initialized the table:

table = new JTable(ctm);
        for (int row=0;row<table.getRowCount();row++) {
            ImageIcon ii = (ImageIcon)table.getValueAt(row, 2);
            table.setRowHeight(row, ii.getIconHeight());
        }
Member Avatar
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

ok thanks lot for you ....

i leave it because the estimation time was expired...

but now i need how do change color of particular row in jtable...

first i show all records in jtable ...

after i process(read from it and insert into DB) one record at a time..

while processing i need that particular row in yellow color...

if the row completed (inserted in DB) THEN the row become GREEN..

HOW TO DO...I TRIED SOME SOLUTION ...BUT IT WILL MAKE WHOLE TABLE AT A TIME...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article