• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image in JTable Cell  RSS feed

 
Vishal Patel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.*;
import javax.swing.*;
import java.util.Vector;
import java.awt.Container;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Dimension;

public class JTableAndDatabase extends JFrame
{
public Vector col,row,rowdata;
public JTable table;

public JTableAndDatabase()
{
Container cnt = getContentPane();

col = new Vector();
row = new Vector();
rowdata = new Vector();

col.addElement("1");
col.addElement("2");
col.addElement("3");
col.addElement("4");

row.addElement("Vishal");
row.addElement("Vishal");
row.addElement("Vishal");
row.addElement(new ImageIcon("gogreen.gif"));
rowdata.addElement(row);

table = new JTable(rowdata,col);

JScrollPane scroll = new JScrollPane(table);

cnt.add(scroll);
setSize(500,500);
show();
}

public static void main(String[] args)
{
JTableAndDatabase demo = new JTableAndDatabase();
}
}



What's wrong with this code?

It is running properly but it can't show the image in the last collumn.

Help me........
Thanks.......
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just make an instance of the imageicon u want to display and try with the following JTable's method. Hope it will work :
=========================================================================
public void setValueAt(Object aValue,
int row,
int column)
Sets the value for the cell in the table model at row and column.
Parameters:
aValue - the new value
row - the row of the cell to be changed
column - the column of the cell to be changed
=========================================================================

let me know if it does...
till then
Yogesh
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing...i forgot to mention.

just check if u r having that image under the same directory as ur java files are.
best practice could be store all the images under 'images' directory which u can have under ur current working directory.
and the u refer ur images as ...say in ur case..."\images\gogreen.gif".


Yogesh Kodgule
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kkkkk lllll,

Welcome to Javaranch. I suspect you forgot to read our Naming Policy when you created your account. Please read it again and adjust your display name accordingly.

You can change your display name here.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!